tags:

views:

82

answers:

6

my php array looks like this:

Array ( 
[0] => dummy 
[1] => stdClass Object ( 
    [aid] => 1 
    [atitle] => Ameya R. Kadam ) 
[2] => stdClass Object ( 
    [aid] => 2 
    [atitle] => Amritpal Singh ) 
[3] => stdClass Object ( 
    [aid] => 3 
    [atitle] => Anwar Syed ) 
[4] => stdClass Object ( 
    [aid] => 4 
    [atitle] => Aratrika )  
) )

now i want to echo the values inside [atitle]. to be specific i want to implode values of atitle into another variable. how can i make it happen?

A: 

What you have there is an object.

You can access [atitle] via

$array[1]->atitle;
metrobalderas
The value the OP wants is not at index 0
Mr-sk
+4  A: 

With PHP 5.3:

$result = array_map(function($element) { return $element->atitle; }, $array);

if you don't have 5.3 you have to make the anonymous function a regular one and provide the name as string.


Above I missed the part about the empty element, using this approach this could be solved using array_filter:

$array = array_filter($array, function($element) { return is_object($element); });
$result = array_map(function($element) { return $element->atitle; }, $array);

If you are crazy you could write this in one line ...

johannes
Not all elements have a title as shown in the sample. This would error.
cballou
Yeah, I missed that in the original post which wasn't formatted properly. I guess an foreach()-loop as in Simon's answer is better in this situation. But let's keep the answer here,as array_map can be quite powerful. But I will also add the array_filter to this example to show the powers of these functions.
johannes
+3  A: 

Your array is declared a bit like this :
(Well, you're probably, in your real case, getting your data from a database or something like that -- but this should be ok, here, to test)

$arr = array(
    'dummy',
    (object)array('aid' => 1, 'atitle' => 'Ameya R. Kadam'), 
    (object)array('aid' => 2, 'atitle' => 'Amritpal Singh'), 
    (object)array('aid' => 3, 'atitle' => 'Anwar Syed'), 
    (object)array('aid' => 4, 'atitle' => 'Aratrika'), 
);


Which means you can extract all the titles to an array, looping over your initial array (excluding the first element, and using the atitle property of each object) :

$titles = array();
$num = count($arr);
for ($i=1 ; $i<$num ; $i++) {
    $titles[] = $arr[$i]->atitle;
}
var_dump($titles);


This will get you an array like this one :

array
  0 => string 'Ameya R. Kadam' (length=14)
  1 => string 'Amritpal Singh' (length=14)
  2 => string 'Anwar Syed' (length=10)
  3 => string 'Aratrika' (length=8)


And you can now implode all this to a string :

echo implode(', ', $titles);

And you'll get :

Ameya R. Kadam, Amritpal Singh, Anwar Syed, Aratrika
Pascal MARTIN
This will throw at least a notice on the 'dummy' element, no?
Simon
Note that this assumes the only array index without a title is the very first one.
cballou
@Simon : I don't think soo (and I don't get any notice with that code) ;; @cballou : indeed ; another solution would be to test, in the loop, if the data of the current line is an object and has a atitle propery
Pascal MARTIN
it gives me an error. Catable fatal error.
amit
A: 

If you want to check for the existence of title before output, you could use:

// generates the title string from all found titles
$str = '';
foreach ($array AS $k => $v) {
    if (isset($v->title)) {
        $str .= $v->title;
    }
}
echo $str;

If you wanted these in an array it's just a quick switch of storage methods:

// generates the title string from all found titles
$arr = array();
foreach ($array AS $k => $v) {
    if (isset($v->title)) {
        $arr[] = $v->title;
    }
}
echo implode(', ', $arr);

stdClass requires you to use the pointer notation -> for referencing whereas arrays require you to reference them by index, i.e. [4]. You can reference these like:

$array[0]
$array[1]->aid
$array[1]->atitle
$array[2]->aid
$array[2]->atitle
// etc, etc.
cballou
A: 
$yourArray = array(); //array from above
$atitleArray = array();

foreach($yourArray as $obj){
 if(is_object($obj)){
    $atitleArray[] = $obj->aTitle;
  }
}

seeing as how not every element of your array is an object, you'll need to check for that.

GSto
+1  A: 
foreach($array as $item){
    if(is_object($item) && isset($item->atitle)){
         echo $item->atitle;
    }
}

to get them into an Array you'd just need to do:

$resultArray = array();
foreach($array as $item){
    if(is_object($item) && isset($item->atitle)){
          $resultArray[] = $item->atitle;
    }
}

Then resultArray is an array of all the atitles

Then you can output as you'd wish

$output = implode(', ', $resultArray);
Simon
You can skip the `is_object` check and just test if `$item->atitle` is set.
cballou
that does it. how do i implode it now?
amit
Post updated to give you an array of the atitles
Simon