How can I loop through this array - PHP - php

This is the var_dump($options)
array (size=4)
0 => string '2' (length=1)
'parent_2_children' =>
array (size=3)
0 => string '22' (length=2)
1 => string '24' (length=2)
2 => string '23' (length=2)
1 => string '3' (length=1)
'parent_3_children' =>
array (size=3)
0 => string '26' (length=2)
1 => string '25' (length=2)
2 => string '27' (length=2)
What I have tried up to now is
if(!is_null($options))
{
foreach($options as $option)
{
if(!array_key_exists('parent_'.$option.'_children',$options))
{
//if position null output an error
}
}
}
Print_r() as requested
Array
(
[0] => 2
[parent_2_children] => Array
(
[0] => 22
[1] => 24
[2] => 23
)
[1] => 3
[parent_3_children] => Array
(
[0] => 26
[1] => 25
[2] => 27
)
)

use print_r($options) in staid of var_dump it's easier to read..
check if you got a numeric key, then check if the new key exists. Throw an error.
if(!is_null($options)) {
foreach($options as $key => $option) {
if(is_int($key) && !array_key_exists('parent_'.$option.'_children',$options)) {
echo 'parent_'.$option.'_children does not exist';
}
}
}
Here is a working example

Your code is correct. An additional check on the nature of the key will reduce the execution, since you have to do the processing only for numeric keys.
if(!is_null($options))
{
foreach($options as $key => $option)
{
if (is_numeric($key)) {
if(!array_key_exists('parent_'.$option.'_children',$options))
{
print 'parent_'.$option.'_children does not exist';
}
}
}
}
To test the code, try the following array :
$options = array(0 => 2, 'parent_2_children' => array ( 0 => 22, 1 => 24, 2 => 23 ), 1 => 3, 'parent_3_children' => array ( 0 => 26, 1 => 25, 2 => 27 ), 2=>4 );
whose print_r output will be :
Array
(
[0] => 2
[parent_2_children] => Array
(
[0] => 22
[1] => 24
[2] => 23
)
[1] => 3
[parent_3_children] => Array
(
[0] => 26
[1] => 25
[2] => 27
)
[2] => 4
)
and, it will output :
parent_4_children does not exist

Try this?
if(!is_null($options)){
foreach($options as $option){
if(!array_key_exists('parent_'.$option.'_children',$options)){
throw new Exception("Something went wrong!");
}
}
}

Related

Randomize elements with same value

Hello stackoverflow community. I need help with arrays. It's my weakness. I've got this kind of array:
Array
(
[0] => Array
(
[id] => 7
[slot] => 1
[name] => Apple
[start_date] => 12/16/2015
[end_date] => 03/10/2016
[status] => 1
[pre_exp_email] => 0
)
[1] => Array
(
[id] => 8
[slot] => 1
[name] => Cherry
[start_date] => 12/29/2015
[end_date] => 03/20/2016
[status] => 1
[pre_exp_email] => 0
)
[2] => Array
(
[id] => 5
[slot] => 3
[name] => Bananna
[start_date] => 11/30/2015
[end_date] => 00/00/0000
[status] => 1
[pre_exp_email] => 0
)
[3] => Array
(
[id] => 1
[slot] => 4
[name] => Kiwi
[start_date] => 11/21/2015
[end_date] => 12/21/2016
[status] => 1
[pre_exp_email] => 0
)
)
And my job is to randomize elements which has same [slot], but leave order ascending. For example now it is:
1 Apple 1 Cherry 3 Bannana 4 Kiwi
I need to randomize those elements who has same slot number. So Apple and Cherry would swap positions. How can I do this stuff?
Update : Using shuffle & usort :
shuffle($fruits);
function cmp($a, $b) {
if ($a['slot'] == $b['slot']) {
return 0;
}
return ($a['slot'] < $b['slot']) ? -1 : 1;
}
usort($fruits, "cmp");
Make a new array from the original having slot as keys
$elements = array(
0 => Array
(
'id' => 7,
'slot' => 1
),
1 => Array
(
'id' => 8,
'slot' => 1
),
2 => Array
(
'id' => 9,
'slot' => 1
),
3 => Array
(
'id' => 9,
'slot' => 5
)
);
foreach($elements as $element){
$newArray[$element['slot']][] = $element; //put every element having the same slot
}
$elementSlots = array_keys($newArray); // all slots are stored in elementSlots
$Result = array();
foreach($elementSlots as $slot) {
shuffle($newArray[$slot]); //randomize elements having the same slot
foreach($newArray[$slot] as $element) { //add them to the result array
$Result[$slot][] = $element;//For output 1
//$Result[] = $element; //For output 2
}
}
var_dump($Result);
Output 1:
array (size=2)
1 =>
array (size=3)
0 =>
array (size=2)
'id' => int 7
'slot' => int 1
1 =>
array (size=2)
'id' => int 9
'slot' => int 1
2 =>
array (size=2)
'id' => int 8
'slot' => int 1
5 =>
array (size=1)
0 =>
array (size=2)
'id' => int 9
'slot' => int 5
Output 2:
array (size=4)
0 =>
array (size=2)
'id' => int 7
'slot' => int 1
1 =>
array (size=2)
'id' => int 9
'slot' => int 1
2 =>
array (size=2)
'id' => int 8
'slot' => int 1
3 =>
array (size=2)
'id' => int 9
'slot' => int 5

Sort php different format multidimensional array on key

I have an array like this
Array
(
[name] => Array
(
[0] => img/test240.jpg
[1] => img/cs1.jpg
[2] => img/cs2.jpg
[3] => img/cs3.jpg
)
[link] => Array
(
[0] => http://google.com
[1] => http://google.com
[2] => http://facebook.com
[3] => http://orkut.com
)
[order] => Array
(
[0] => 4
[1] => 1
[2] => 2
[3] => 3
)
)
I need to sort it by order WHICH IS KEY in Multidimensional array. Here is output.
Array
(
[name] => Array
(
[1] => img/cs1.jpg
[2] => img/cs2.jpg
[3] => img/cs3.jpg
[0] => img/test240.jpg
)
[link] => Array
(
[1] => http://google.com
[2] => http://facebook.com
[3] => http://orkut.com
[0] => http://google.com
)
[order] => Array
(
[1] => 1
[2] => 2
[3] => 3
[0] => 4
)
)
In this you can see when order is sorted name and link is also sorted according to the order. How can i do this with php.
You have to use array_map() in conjunction with sort().
If you want to preserve actual element order you have to use asort() instead sort().
Try this code:
$arr = array(
'name' => array(
0 => 'img/test240.jpg',
1 => 'img/cs1.jpg',
2 => 'img/cs2.jpg',
3 => 'img/cs3.jpg',
),
'link' => array(
0 => 'http://google.com',
1 => 'http://google.com',
2 => 'http://facebook.com',
3 => 'http://orkut.com',
),
'order' => array(
0 => 4,
1 => 1,
2 => 2,
3 => 3,
),
);
function mysort($a) {
asort($a);
return $a;
}
$arr = array_map('mysort', $arr);
print_r($arr);
Demo.
Try this, it uses array_multisort:
$array holds:
array (size=3)
'name' =>
array (size=4)
0 => string 'img/test240.jpg' (length=15)
1 => string 'img/cs1.jpg' (length=11)
2 => string 'img/cs2.jpg' (length=11)
3 => string 'img/cs3.jpg' (length=11)
'link' =>
array (size=4)
0 => string 'http://google.com' (length=17)
1 => string 'http://google.com' (length=17)
2 => string 'http://facebook.com' (length=19)
3 => string 'http://orkut.com' (length=16)
'order' =>
array (size=4)
0 => string '4' (length=1)
1 => string '1' (length=1)
2 => string '2' (length=1)
3 => string '3' (length=1)
Code:
$sort = array();
foreach($array as $k) {
foreach($k as $ind=>$val){
$sort['name'][$ind] = $array['name'][$ind];
$sort['link'][$ind] = $array['link'][$ind];
$sort['order'][$ind] = $array['order'][$ind];
}
}
array_multisort($sort['order'], SORT_ASC, $sort['link'], SORT_ASC, $sort['name'], SORT_ASC);
var_dump($sort);
Output:
array (size=3)
'name' =>
array (size=4)
0 => string 'img/cs1.jpg' (length=11)
1 => string 'img/cs2.jpg' (length=11)
2 => string 'img/cs3.jpg' (length=11)
3 => string 'img/test240.jpg' (length=15)
'link' =>
array (size=4)
0 => string 'http://google.com' (length=17)
1 => string 'http://facebook.com' (length=19)
2 => string 'http://orkut.com' (length=16)
3 => string 'http://google.com' (length=17)
'order' =>
array (size=4)
0 => string '1' (length=1)
1 => string '2' (length=1)
2 => string '3' (length=1)
3 => string '4' (length=1)
$this_arr = array(1,2,3,0);
function my_sort_2($arr, $arrangement)
{
$flag = false;
foreach($arr as $key => $val)
{
if(is_array($arr[$key]))
{
$arr[$key] = my_sort_2($arr[$key],$arrangement);
$flag = true;
}
}
if($flag == false && is_array($arr) && is_assoc($arr) === false)
{
$temp = array();
for($i = 0; $i < count($arrangement); $i++)
{
if(isset($arr[$arrangement[$i]]))
{
$temp[$arrangement[$i]] = $arr[$arrangement[$i]];
unset($arr[$arrangement[$i]]);
}
}
//$arr = array_merge($temp,$arr);
$arr = $temp;
}
return $arr;
}
Include this function below to run my own function. Also credit to #Matt Whittingham where i got this code from
function is_assoc($array)
{
$keys = array_keys($array);
return array_keys($keys) !== $keys;
}
Now let's do some sortin'... print_r(my_sort_2($arr,$this_arr)); assuming $arr contains Shan's array.
The output is EXACTLY what you desired.
It'll search for nested array (at least intended) and see if it's in a standard numeric ordered keys (in short, not custom order - yet; and not assoc) then sort it the way you want.
Note: I know my code isn't that probably good, optimized or bug free and that's my second attempt, misunderstanding your requirements first time (see the function name?).
Well after some research i found a simple solution like this
asort($data['order']);
$keys = array_keys($data['order']);
$data['name'] = array_replace(array_flip($keys), $data['name']);
$data['link'] = array_replace(array_flip($keys), $data['link']);
$data['order'] = array_replace(array_flip($keys), $data['order']);
Although i dont want to apply array_replace and array_flip on all the keys but this is done for the time being. I will surely trying to find how i can do it with single instruction.

Remove a value from an array and put that value into another array

i have two arrays and i need to extract the values of the 2nd array depending on the value of $arr[0]["num"]
$arr = array(
0 => array(
"id" => 24,
"num" => 2
),
1 => array(
"id" => 25,
"num" => 5
)
2 => array(
"id" => 26,
"num" => 3
)
);
$array = array('1','2','3','4','5','6','7','8','9','10');
$new = array();
foreach($arr as $key){
for($i=0;$i<$key['num'];$i++){
$new[$key['id']][$i] = $array[$i];
}
}
is it possible to remove the values of the 2nd array and transfer it into a new array?
what my loop does is just copying the values from the start after each loop. i want to remove the copied values from the 2nd array.
The output should be like this:
Array
(
[24] => Array
(
[0] => 1
[1] => 2
)
[25] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 6
[4] => 7
)
[26] => Array
(
[0] => 8
[1] => 9
[2] => 10
)
)
I'd suggest using array_shift
$arr = array(
array(
"id" => 24,
"num" => 2
),
array(
"id" => 25,
"num" => 5
),
array(
"id" => 26,
"num" => 3
)
);
$array = array('1','2','3','4','5','6','7','8','9','10');
$new = array();
foreach($arr as $key){
for($i=0;$i<$key['num'];$i++){
$new[$key['id']][$i] = $array[0]; // *1
array_shift($array);
}
}
echo '<pre>';
print_r($new);
*1 You have to change this line as well. Since array_shift removes the first array entry, each iteration should access array[0].
Output:
Array
(
[24] => Array
(
[0] => 1
[1] => 2
)
[25] => Array
(
[0] => 3
[1] => 4
[2] => 5
[3] => 6
[4] => 7
)
[26] => Array
(
[0] => 8
[1] => 9
[2] => 10
)
)
Try this
foreach($arr as $key){
for($i=0;$i<$key['num'];$i++){
$new[$key['id']][$i] = $array[$i];
// unset previous values, in first iteration it will remove 0, 1
unset($array[$i]);
}
// reset the array keys, so for loop $i will start from 0
$array = array_values($array);
}
Output:
array (size=3)
24 =>
array (size=2)
0 => string '1' (length=1)
1 => string '2' (length=1)
25 =>
array (size=5)
0 => string '3' (length=1)
1 => string '4' (length=1)
2 => string '5' (length=1)
3 => string '6' (length=1)
4 => string '7' (length=1)
26 =>
array (size=3)
0 => string '8' (length=1)
1 => string '9' (length=1)
2 => string '10' (length=2)

PHP - compare and filter two arrays with different dimensions

I am trying to filter two arrays to get a final result with user ids from my mysql database
I have two arrays the first one:
print_r($arr_partner_id);
Array (
[0] => Array ( [id] => 335 [id_partner] => 0 )
[1] => Array ( [id] => 469 [id_partner] => 1 )
[2] => Array ( [id] => 457 [id_partner] => 1 )
[3] => Array ( [id] => 339 [id_partner] => 0 )
[4] => Array ( [id] => 361 [id_partner] => 0 ) )
and the second one:
print_r($arr_member_id);
Array (
[0] => 457
[1] => 469
[2] => 339
[3] => 361 )
now i want compare these two only with their ids and delete the ids that are not included in the "$arr_member_id" Array. This my "reference Array" that means i only need the ids (457,469,339,361)
for the final result it should be looking like this:
print_r($arr_partner_final_id);
Array (
[0] => Array ( [id] => 469 [id_partner] => 1 )
[1] => Array ( [id] => 457 [id_partner] => 1 )
[2] => Array ( [id] => 339 [id_partner] => 0 )
[3] => Array ( [id] => 361 [id_partner] => 0 ) )
i tryed it with foreach
foreach ($arr_partner_id as $key => $usr_ids) {
if($arr_partner_id[$key]['id'] == $arr_member_id[$key]) {
// do something
}
}
but the "keys" are different this should not working...
making it as simple, and using just one loop to loop through the array and checkin if the id is present in another set of array using in_array()
try this
for($i=0;$i<count($arr_partner_id);$i++){
if(!in_array($arr_partner_id[$i]['id'],$arr_member_id)){
unset($arr_partner_id[$i]);
}
}
print_r($arr_partner_id);
try it here
AND yes!! if you want seperate arrays for that then simply modify the code..create new array and push the elements that is present in array
$finalArray=array();
for($i=0;$i<count($arr_partner_id);$i++){
if(in_array($arr_partner_id[$i]['id'],$arr_member_id)){
$finalArray[]=$arr_partner_id[$i];
}
}
print_r($finalArray);
Try this (Working example : http://codepad.org/ApFcA3Zo)
<?php
$arr_partner_id=array (
'0' => array ( 'id' => 335, 'id_partner' => 0 ) ,
'1' => array ( 'id' => 469, 'id_partner' => 1 ) ,
'2' => array ( 'id' => 457, 'id_partner' => 1 ) ,
'3' => array ( 'id' => 339, 'id_partner' => 0 ) ,
'4' => array ( 'id' => 361, 'id_partner' => 0 ) ) ;
$arr_member_id=array (
'0' => 457 ,
'1' => 469 ,
'2' => 339 ,
'3' => 361 ) ;
$final =array();
foreach($arr_partner_id as $arr)
{
foreach($arr_member_id as $parr)
{
if($arr['id'] == $parr)
{
$final[]=$arr;
}
}
}
print_r($final);
?>
Maybe something like:
foreach ($arr_member_id as $usr_id){
foreach ($arr_partner_id as $partner){
if ($usr_id == $partner['id']) {$arr_partner_final_id[]=$partner;break;
}
}
Another solution (without explicit looping):
$arr_partner_id=array (
'0' => array( 'id' => 335, 'id_partner' => 0 ),
'1' => array( 'id' => 469, 'id_partner' => 1 ),
'2' => array( 'id' => 457, 'id_partner' => 1 ),
'3' => array( 'id' => 339, 'id_partner' => 0 ),
'4' => array( 'id' => 361, 'id_partner' => 0 ));
$arr_member_id=array (
'0' => 457,
'1' => 469,
'2' => 339,
'3' => 361);
function compare($v){global $arr_member_id;return in_array($v['id'], $arr_member_id);}
var_dump($arr_partner_id = array_filter($arr_partner_id, 'compare'));
better you use mysql itself do this task. but if you need to continue with this use in array function to check the second array as given below,
foreach ($arr_partner_id as $key => $usr_ids) {
if(in_array($usr_ids["id"], $arr_member_id)) {
// do something
}
}

Issue with recursive loop and nested multidimensional array

I have here a nested multidimensional array:
Array
(
[merchant] => Array
(
[XML_Serializer_Tag] => Array
(
[id] => 736
[name] => Cadbury Gifts Direct
)
[prod] => Array
(
[XML_Serializer_Tag] => Array
(
[0] => Array
(
[XML_Serializer_Tag] => Array
(
[id] => 88966064
[pre_order] => no
[web_offer] => no
[in_stock] => no
[stock_quantity] => 0
)
[pId] => 608
[isbn] => 0000000000000
[text] => Array
(
[name] => 50% OFF 56g Creme Egg Minis
[desc] => 50% OFF Creme Egg Minis in a 56g bag.
)
[uri] => Array
(
[awTrack] => http://www.awin1.com/pclick.php?p=88966064&a=67702&m=736
[awThumb] => http://images.productserve.com/thumb/736/88966064.jpg
[awImage] => http://images.productserve.com/preview/736/88966064.jpg
[mLink] => http://www.cadburygiftsdirect.co.uk/products/608-50-off-56g-creme-egg-minis.aspx
[mImage] => http://www.cadburygiftsdirect.co.uk/images/thumbs/0001084.png
)
[price] => Array
(
[XML_Serializer_Tag] => Array
(
[curr] => GBP
)
[buynow] => 0.31
[store] => 0.00
[rrp] => 0.00
[delivery] => 0.00
)
[cat] => Array
(
[awCatId] => 437
[awCat] => Chocolate
[mCat] => Full Range
)
[brand] =>
[valFrom] => 0000-00-00
[valTo] => 0000-00-00
[comAmount] => 0.00
)
The segment loop afterwards.
So...
[1] => Array
[2] => Array
[3] => Array
etc...
I need to find the names of the attributes of each array segment.
So I have used this recursive loop:
private function recursive_array($old_array, $new_array = array()) {
foreach ($old_array as $key => $value) {
if (is_array($value) && $key < 1) {
$new_array = $this->recursive_array($value, $new_array);
} else {
if ($key < 1) {
$new_array[] = $key;
}
}
}
return $new_array;
}
This is the output:
array
0 => string 'id' (length=2)
1 => string 'name' (length=4)
2 => string 'id' (length=2)
3 => string 'pre_order' (length=9)
4 => string 'web_offer' (length=9)
5 => string 'in_stock' (length=8)
6 => string 'stock_quantity' (length=14)
7 => string 'pId' (length=3)
8 => string 'isbn' (length=4)
9 => string 'name' (length=4)
10 => string 'desc' (length=4)
11 => string 'awTrack' (length=7)
12 => string 'awThumb' (length=7)
13 => string 'awImage' (length=7)
14 => string 'mLink' (length=5)
15 => string 'mImage' (length=6)
16 => string 'curr' (length=4)
17 => string 'buynow' (length=6)
18 => string 'store' (length=5)
19 => string 'rrp' (length=3)
20 => string 'delivery' (length=8)
21 => string 'awCatId' (length=7)
22 => string 'awCat' (length=5)
23 => string 'mCat' (length=4)
24 => string 'brand' (length=5)
25 => string 'valFrom' (length=7)
26 => string 'valTo' (length=5)
27 => string 'comAmount' (length=9
What it is also picking up is the top nested array:
[XML_Serializer_Tag] => Array
(
[id] => 736
[name] => Cadbury Gifts Direct
)
This is just details for the entire feed not the individual segments.
I need a way of filtering out the top nested array but bear in mind that the details are dynamic so the name of the keys can change from one feed to the next
Maybe this?
$riter = new RecursiveIteratorIterator(New RecursiveArrayIterator($array));
foreach ($riter as $key => $val) {
if ($riter->getDepth() > 1) {
echo "$key => $val\n";
}
}
by default, RecursiveIteratorIterator only visits leaf nodes(the deepest levels down every path). getDepth() can be used to make sure were a minimum depth. I'm not sure if 1 is the correct number, but anyway...
use array_shift to pop off the first element of the array

Categories