Viewed   271 times

In terms of my data structure, I have an array of communications, with each communications_id itself containing three pieces of information: id, score, and content.

I want to implode this array in order to get a comma separated list of ids, how do I do this?

 Answers

5

Update for PHP 5.5

PHP 5.5 introduces array_column which is a convenient shortcut to a whole class of array_map usage; it is also applicable here.

$ids = array_column($communications, 'id');
$output = implode(',', $ids);

Original answer

You need to make an array of just ids out of your array of communications. Then the implode would be trivial.

Hint: the function for that is array_map.

Solution:

Assumes PHP 5.3, otherwise you 'd have to write the callback as a string.

$ids = array_map(function($item) { return $item['id']; }, $communications);
$output = implode(',', $ids);
Tuesday, September 20, 2022
5

I played with your code to get it working :

function findKey($array, $keySearch)
{
    foreach ($array as $key => $item) {
        if ($key == $keySearch) {
            echo 'yes, it exists';
            return true;
        } elseif (is_array($item) && findKey($item, $keySearch)) {
            return true;
        }
    }
    return false;
}
Friday, December 16, 2022
2

Try below code:

<?php

$arr = array(
        array('city' => 'NewYork', 'cash' => '1000'),
        array('city' => 'Philadelphia', 'cash' => '2300'),
        array('city' => 'NewYork', 'cash' => '2000'),
    );

$newarray = array();
foreach($arr as $ar)
{
    foreach($ar as $k => $v)
    {
        if(array_key_exists($v, $newarray))
            $newarray[$v]['cash'] = $newarray[$v]['cash'] + $ar['cash'];
        else if($k == 'city')
            $newarray[$v] = $ar;
    }
}

print_r($newarray);


Output:

Array
(
    [NewYork] => Array
        (
            [city] => NewYork
            [cash] => 3000
        )

    [Philadelphia] => Array
        (
            [city] => Philadelphia
            [cash] => 2300
        )

)


Demo:
http://3v4l.org/D8PME

Saturday, August 6, 2022
2

I updated your function and it is giving expected output. Please give a try as following.

function array_key_exists_r($needle, $haystack){
    $result = array_key_exists($needle, $haystack);
    if ($result) 
    {
        foreach ($haystack as $a=>$v) 
        {
            if($needle == $a)
                return $haystack[$a];
            if (is_array($v)) {
                $result = array_key_exists_r($needle, $v);
            }
            if ($result) return $result;
        }
    }
    foreach ($haystack as $v) {
        if (is_array($v)) {
            $result = array_key_exists_r($needle, $v);
        }
        if ($result) return $result;
    }
    return $result;
};
Thursday, December 1, 2022
 
asaph
 
2

The actual problem is you're not assigning the result of array_reverse to anything

// change this
array_reverse($respArr);

// to this
$respArr = array_reverse($respArr);
Friday, August 19, 2022
 
benvd
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :