Array format Conversion in PHP - php

I have got an array which looks like the following:
Array ( [0] => Array ( [id] => 0 ) [1] => Array ( [id] => 0 ) [2] => Array ( [id] => 0 ) [3] => Array ( [id] => 0 ) [4] => Array ( [id] => 0 ) [5] => Array ( [id] => 0 ) [6] => Array ( [id] => 0 ) [7] => Array ( [id] => 0 ) [8] => Array ( [id] => 0 ) [9] => Array ( [id] => 0 ) [10] => Array ( [id] => 0 ) [11] => Array ( [id] => 4 ) [12] => Array ( [id] => 1 ) [13] => Array ( [id] => 2 ) [14] => Array ( [id] => 0 ) [15] => Array ( [id] => 0 ) [16] => Array ( [id] => 5 ) [17] => Array ( [id] => 1 ) )
and I would like to convert it into the following format:
[0,0,0,0,0,0,0,0,0,0,0,4,1,2,0,0,5,1]
How can I achieve this conversion using php?
All the help is appreciated.

PHP >= 5.5.0 needed for array_column() or use the PHP Implementation of array_column():
$result = array_column($array, 'id');

Simplest way: (assuming that you want it to actually output the string [5,6,...,2]
<?php
$ids=array();
foreach($yourArray as $k=>$v){
$ids[] = $v['id'];
}
$resultCsv = implode(',', $ids);
$result = '['.$resultCsv.']';
echo $result
?>

You can use array_map or array_walk:
With array_map:
$new = array_map(function ($value) { return $value['id']; }, $theArray);
print_r($new);
With array_walk:
array_walk($theArray, function (&$value, $key) { $value = $value['id']; };
print_r($theArray);
The main differences between these is that array_map produces a completely new array, and you dont get the key passed to the callback function. array_walk on the other hand works on the array by reference and the callback also gets the key.
Now its unclear to me whether you wanted the array restructured (as i provided solutions for) or if you want it in a specific string format like the [0,1,2,3,...] you provided. If you did want it in a string format all you need to do now is call json_encode on it:
// note if you used array_map pass the variable that holds the result of the call
// (in my example that would be $new); If you used array_walk then pass the same array
// you supplied as the argument to the call (in my exmaple that would be $theArray)
$arrString = json_encode($theArray);

You can process every member of the array and append every currently processed sub-array's member "id" to a new array:
<?php
// code
foreach $array as $member {
$newarray[]=$member["id"];
}
// code
?>

Related

How to remove duplicate values from a multi dimensional array for specific key in php [duplicate]

This question already has answers here:
How to remove duplicate values from a multi-dimensional array in PHP
(18 answers)
Closed 2 years ago.
I searched for solutions on here but didn't find one for my use case.
I have a big array which is built like this example:
Array
(
[0] => Array
(
[Template] => page.html5
)
[1] => Array
(
[Template] => page2.html5
)
[2] => Array
(
[Template] => page.html5
)
[3] => Array
(
[Template] => page2.html5
)
[4] => Array
(
[Template] => page.html5
)
[5] => Array
(
[Template] => page2.html5
)
[6] => Array
(
[id] => 27
[table] => tl_custom
[type] => text
[data] => Array
(
[fragment] => example
[previewId] => 1
[isActive] => 1
)
)
)
I would like to remove all duplicate values for the array key "Template", but besides that I want the array to stay the way it is.
So afterwards my Array should look like:
Array
(
[0] => Array
(
[Template] => page.html5
)
[1] => Array
(
[Template] => page2.html5
)
[6] => Array
(
[id] => 27
[table] => tl_custom
[type] => text
[data] => Array
(
[fragment] => example
[previewId] => 1
[isActive] => 1
)
)
)
Is there a way to achieve this without using lots of memory?
Thanks for your answers :)
You could use the following logic, which uses:
array_map() to flatten the array with index keys-values, and serialize() (stringify) the last array element so we can use
array_unique() on the result.
Then, to restore the stringified array, i.e. turn it back into an array, we use unserialize().
<?php
$newArr = array_unique(array_map(function ($el) {
return $el['Template'] ?? serialize($el);
}, $arr));
// restore the last element to array
$last = array_key_last($newArr); // (PHP 7 >= 7.3.0)*
$newArr[$last] = unserialize($newArr[$last]);
*if PHP version <7.3.0 use: end($newArr); $last = key($newArr);
Output:
Array
(
[0] => page.html5
[1] => page2.html5
[6] => Array
(
[id] => 27
[table] => tl_custom
[type] => text
[data] => Array
(
[fragment] => example
[previewId] => 1
[isActive] => 1
)
)
)
working demo
The code below loops the array, marks indexes for removal and then another loop does the removals:
$templates = array(); //This will store the remove plan
for ($index = 0; $index < count($input); $index++) {
if (isset($input[$index]["Template"])) { //Ignore items where there is no template
if (isset($templates[$input[$index]["Template"]])) { //Let's check whether we have already seen this template
$templates[$input[$index]["Template"]] = array(); //From now on we will find duplicates for this dude
} else { //Mark for removal
$templates[$input[$index]["Template"]][]=$index;
}
}
}
//Actual removals
foreach($templates => $index) {
//Removing the actual element:
unset($input[$index]["Template"]);
//Remove the parent as well if it becomes empty
if (!count($input[$index])) unset($input[$index]);
}
The memory need for this algorithm is:
average(element_size) * number_of_elements

Array structure rearrangement without loop

I have an array like mentioned below, which I want to rearrange without using loop:
Array
(
[0] => Array
(
[Books] => Array
(
[id] => 4
)
)
[1] => Array
(
[Books] => Array
(
[id] => 3
)
)
[2] => Array
(
[Books] => Array
(
[id] => 2
)
)
[3] => Array
(
[Books] => Array
(
[id] => 1
)
)
)
I want an output like this:
Array(4,3,2,1)
I'm assuming you do not want to use for or foreach loops, but anything else that internally is or uses a loop is fine.
in this case, you can use array_map:
$result = array_map(function($item){
return $item['books']['id'];
}, $currentArray);
OR
if you do not even want that:
$v1 = array_column($input, 'books');
$result = array_column($v1, 'id');

PHP - Merge 2 arrays of object using a key/id

I want to merge the 2 arrays of objects based on the 'id' field of Array1 and the 'itemVendorCode' of Array2. I also wanted to remove from the resulting arrays of object anything that didn't match.
Array1:
Array
(
[0] => stdClass Object
(
[id] => 10-423-1176
[qty] => 2
[price] => 12.6
)
[1] => stdClass Object
(
[id] => 89-575-2354
[qty] => 24
[price] => 230.35
)
[2] => stdClass Object
(
[id] => 89-605-1250
[qty] => 2
[price] => 230.35
)
)
Array2:
Array
(
[0] => Item Object
(
[internalId] => 14062
[itemVendorCode] => 89-605-1250
)
[1] => Item Object
(
[internalId] => 33806
[itemVendorCode] => 89-575-2354
)
[2] => Item Object
(
[internalId] => 64126
[itemVendorCode] => 26-295-1006
)
)
I was able to solve this by this code:
$indexed = array();
foreach($itemsArray as $value) {
$indexed[$value->itemVendorCode] = $value;
}
$results = array();
foreach($vendorItems as $obj) {
$value = $indexed[$obj->id];
if (isset($value)) {
foreach($value as $name => $val) {
$obj->$name = $val;
array_push($results, $obj);
}
}
}
print_r($results);
credits to the original poster. I just modified it a bit,
I was able to get the result like this:
Array
(
[0] => stdClass Object
(
[id] => 10-423-1176
[qty] => 2
[price] => 12.6
[internalId] => 2035
[itemVendorCode] => 10-423-1176
)
[1] => stdClass Object
(
[id] => 10-423-1176
[qty] => 2
[price] => 12.6
[internalId] => 2035
[itemVendorCode] => 10-423-1176
)
[2] => stdClass Object
(
[id] => 14-102-1010
[qty] => 16
[price] => 3.2
[internalId] => 57033
[itemVendorCode] => 14-102-1010
)
)
I think you will have to use array_map function which provides you a callback function to execute on array(s).
In the callback function:
- declare your array1
- foreach the second array
- set an if statement to check that the current iteration with the id value matches the itemVendorCode of the array2 and return it
something like this:
// You have to specify to PHP to use a local copy of your $array2 to works with it into your callback
$cb = function ($obj1) use ($array2)
{
// you foreach this array
foreach ($array2 as $obj2) {
// if the value of id matches itemVendorCode
if ($obj1->id === $obj2->itemVendorCode) {
// you return the id
return $obj->id;
}
}
};
// this function will fill a new array with all returned data
$mergedArray = array_map($cb, $array1);
This code is a sample but doesn't provide you, your needled solution, try to update it to do what you exactly want ;)

Opposite function of strpos to grab values that do not match a specific pattern in a string or array

What is a function I can use that's basically the opposite of doing
if(strpos($array['some_key'], $value)!==false) {
that means there's a match and confinue
}
I basically want to loop through two arrays and grab the ones that don't have a match to the $value in $array.
$array =
Array
(
[0] => GPPZ20
[1] => GPPZ45
[2] => GPPZ75
[3] => GPPZH20
[4] => GPPZH45
)
$codes =
Array
(
[0] => Array
(
[count] => 1
[code] => GPPZH20SWYE4A2VZU
[amount] => 20
)
)
Array
(
[0] => Array
(
[count] => 1
[code] => GPPZH2077434178J6
[amount] => 20
)
)
Array
(
[0] => Array
(
[count] => 17
[code] => PMMC4
[amount] => 25
)
)
Array
(
[0] => Array
(
[count] => 1
[code] => GPPZH2052910M8V62
[amount] => 20
)
)
Array
(
[0] => Array
(
[count] => 1
[code] => GPPZH45B3116LD1VW
[amount] => 45
)
)
so what i want to do is grab all the ones in the $codes array where the $codes['code'] value does not match any of the ones in the $array value.
right now i have the ones that match and grabbing those by doing
foreach($codes as $code) {
foreach($array as $key=>$value) {
if(strpos($code['code'], $value)!==false) {
//it matches grab those values
}
}
}
I basically now need something like this to grab the ones that do not match
You should use array_filter function - http://php.net/manual/en/function.array-filter.php e.g.:
function myFilter($val){ return strpos('foo', $val) === false; }
$array = array("foobar", "foo", "bar");
var_dump(array_filter($array, myFilter));
You can also use preg_match method instead of strpos.

Count occurrences of a value in a column of an array of object arrays

Does anyone know how to count the occurrences of "photo" in this array:
Array (
[0] => stdClass Object ( [type] => photo [id] => 1404781893036 [created_time] => 2012-03-02T07:58:23+0000 )
[1] => stdClass Object ( [type] => photo [id] => 14047818930362 [created_time] => 2012-03-01T14:58:53+0000 )
[2] => stdClass Object ( [type] => status [id] => 1404781893036 [created_time] => 2012-03-01T09:49:40+0000 )
[3] => stdClass Object ( [type] => status [id] => 14047818930362 [created_time] => 2012-03-01T09:36:04+0000 )
[4] => stdClass Object ( [type] => photo [id] => 14047818930362 [created_time] => 2012-02-28T07:03:25+0000 )
[5] => stdClass Object ( [type] => photo [id] => 1404781893036 [created_time] => 2012-02-27T09:15:34+0000 )
[6] => stdClass Object ( [type] => photo [id] => 14047818930362 [created_time] => 2012-02-27T07:32:13+0000 )
[7] => stdClass Object ( [type] => status [id] => 1404781893036 [created_time] => 2012-02-25T09:36:57+0000 )
[8] => stdClass Object ( [type] => photo [id] => 1404781893036 [created_time] => 2012-02-23T08:46:43+0000 )
[9] => stdClass Object ( [type] => status [id] => 1404781893036 [created_time] => 2012-02-22T21:04:30+0000 )
[10] => stdClass Object ( [type] => status [id] => 1404781893036 [created_time] => 2012-02-21T20:38:27+0000 )
[11] => stdClass Object ( [type] => photo [id] => 1404781893036 [created_time] => 2012-02-21T07:22:44+0000 )
[12] => stdClass Object ( [type] => status [id] => 14047818930362 [created_time] => 2012-02-20T08:32:46+0000 )
[13] => stdClass Object ( [type] => status [id] => 1404781893036 [created_time] => 2012-02-17T15:00:11+0000 ) )
To count matching occurance of a string in multidimensional array you will need to iterate over each array element and match the string and increment the count. Similiarly #Dor has suggested
$count = 0;
foreach ($array as $item) {
if ($item->type === 'photo') {
$count++;
}
}
If you want achieve same in single dimensional array then It's pretty straightforward. You can use array_count_values PHP array function as explained below.
<?php
$array = array(1, "test", 1, "php", "test");
print_r(array_count_values($array));
?>
The above example will output:
Array
(
[1] => 2
[test] => 2
[php] => 1
)
$count = 0;
foreach ($array as $item) {
if ($item->type === 'photo') {
$count++;
}
}
I'd like to acknowledge that the method by Dor Shemer is (IMO) the most direct, clean, readable, and reliable method. I just want to offer a few alternatives for those who prefer to use functional programming ...array_reduce() is a close second for me. Finally, I want to pinpoint a small gotcha for methods that use array_count_values() -- please read on...
Battery of Methods: (Demo)
$photo_count=0; // establish default value
foreach($array as $objects){
if($objects->type==='photo') ++$photo_count; // pre-increment
}
echo "foreach result = $photo_count";
echo "array_reduce = ",array_reduce($array,function($carry,$objects){return $carry+($objects->type==='photo'?1:0);},0);
echo "array_filter & count = ",sizeof(array_filter($array,function($objects){return $objects->type==='photo';}));
echo "array_column & array_filter & count = ",sizeof(array_filter(array_column($array,'type'),function($v){return $v==='photo';}));
echo "array_map & array_count_values & array_replace = ",array_replace(['photo'=>0],array_count_values(array_map(function($o) {return $o->type;}, $array)))['photo'];
echo "array_map & array_count_values (gives Notice) = ",array_count_values(array_map(function($o) {return $o->type;}, $array))['photo'];
Input/Output using OP's sample data (no trouble):
$array=[
(object)['type'=>'photo','id'=>1404781893036,'created_time'=>'2012-03-02T07:58:23+0000'],
(object)['type'=>'photo','id'=>14047818930362,'created_time'=>'2012-03-01T14:58:53+0000'],
(object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-03-01T09:49:40+0000'],
(object)['type'=>'status','id'=>14047818930362,'created_time'=>'2012-03-01T09:36:04+0000'],
(object)['type'=>'photo','id'=>14047818930362,'created_time'=>'2012-02-28T07:03:25+0000'],
(object)['type'=>'photo','id'=>1404781893036,'created_time'=>'2012-02-27T09:15:34+0000'],
(object)['type'=>'photo','id'=>14047818930362,'created_time'=>'2012-02-27T07:32:13+0000'],
(object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-25T09:36:57+0000'],
(object)['type'=>'photo','id'=>1404781893036,'created_time'=>'2012-02-23T08:46:43+0000'],
(object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-22T21:04:30+0000'],
(object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-21T20:38:27+0000'],
(object)['type'=>'photo','id'=>1404781893036,'created_time'=>'2012-02-21T07:22:44+0000'],
(object)['type'=>'status','id'=>14047818930362,'created_time'=>'2012-02-20T08:32:46+0000'],
(object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-17T15:00:11+0000']
];
// output:
foreach result = 7
array_reduce = 7
array_filter & count = 7
array_column & array_filter & count = 7
array_map & array_count_values & array_replace = 7
array_map & array_count_values = 7
Input/Output using data with no photo values (trouble with 2nd array_count_values() method):
$array=[
(object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-03-01T09:49:40+0000'],
(object)['type'=>'status','id'=>14047818930362,'created_time'=>'2012-03-01T09:36:04+0000'],
(object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-25T09:36:57+0000'],
(object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-22T21:04:30+0000'],
(object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-21T20:38:27+0000'],
(object)['type'=>'status','id'=>14047818930362,'created_time'=>'2012-02-20T08:32:46+0000'],
(object)['type'=>'status','id'=>1404781893036,'created_time'=>'2012-02-17T15:00:11+0000']
];
// or if there are no object rows like: $array=[];
// output:
foreach result = 0
array_reduce = 0
array_filter & count = 0
array_column & array_filter & count = 0
array_map & array_count_values & array_replace = 0
array_map & array_count_values (gives Notice) = <br />
<b>Notice</b>: Undefined index: photo in <b>[...][...]</b> on line <b>43</b><br />
array_count_values() doesn't bother to generate elements with a 0 count.
Try with:
$input = array( /* your data */ );
$count = 0;
foreach ( $input as $value ) {
if ( $value->type == 'photo' ) {
$count++;
}
}

Categories