Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

PHP Array question WordPress

  • SOLVED

I have a few custom tables in my database and I need to echo some values but not sure how to.

This is how part of the array looks when I Print it


<?php
$id = 19200;
$myrows = $wpdb->get_results( "SELECT * FROM _isContact WHERE Id='".$id."'");

<pre>
print_r ($myrows);
</pre>
?>





Array
(
[0] => stdClass Object
(
[Id] => 19200
[meta_field] => Address2Street1
[meta_value] => 2400 N Merritt Loop
)

[1] => stdClass Object
(
[Id] => 19200
[meta_field] => Address2Street2
[meta_value] => Suite A
)

[2] => stdClass Object
(
[Id] => 19200
[meta_field] => City
[meta_value] => Tempe
)

[3] => stdClass Object
(
[Id] => 19200
[meta_field] => City2
[meta_value] => Tempe
)

[4] => stdClass Object
(
[Id] => 19200
[meta_field] => CompanyID
[meta_value] => 0
)


Basically how do I get the meta_value of a meta_field that equals say CompanyID. The array numbers change because I will be creating more values so I cant use $companyid = $myrows[4]->meta_value because it may not always be [4]

Thanks!

Answers (4)

2011-10-09

Julian Lannigan answers:

Add this just after the query.


$custom = array();
foreach ($myrows as $item) {
$custom[$item->meta_field] = $item->meta_value;
}


And then you can use $custom["CompanyID"] to get that meta field.

2011-10-09

Maor Barazany answers:

foreach ($myrows as $row) {
if $row->meta_field == 'CompanyID';
{
$value = $row->meta_value;
}
}

echo $value;


Maor Barazany comments:

And if you want to use all the different meta_keys you have, you can use this:

$values = array();
foreach ($myrows as $row) {
$values[$row->meta_field] = $row->meta_value;
}

Than you will have the values in the array as keys -
$custom['CompanyID']
$custom['City']
$custom['Address2Street1']

etc...


Maor Barazany comments:

In these -
$values['CompanyID']
$values['City']
$values['Address2Street1']

2011-10-09

Romel Apuya answers:

2011-10-09

ej_emman answers:

I think you need this..
<?php
foreach ($myrows as $echo_some_rows)
{
if ($echo_some_rows->meta_field == 'city2'); //your conditional tags to display rows
{
echo 'theID-> ' .$echo_some_rows->ID;
echo 'theMetafield-> '.$echo_some_rows->meta_field;
echo 'themetavalue->'. $echo_some_rows->meta_value;
echo '<br>';
}

}
?>