remove duplicates from the two array in php - php

I have stuck to the following problem of removing the duplcation of array
$result=array(2) { [0]=> object(stdClass)#489 (5) { ["id"]=> string(2) "64" ["block_from_id"]=> string(3) "117" ["block_to_id"]=> string(3) "329" ["block_status"]=> string(1) "0" ["username"]=> string(5) "pppoo" } [1]=> object(stdClass)#490 (5) { ["id"]=> string(2) "65" ["block_from_id"]=> string(3) "117" ["block_to_id"]=> string(3) "590" ["block_status"]=> string(1) "0" ["username"]=> string(3) "Pet" } }
$customerlist= array(7) { [0]=> object(stdClass)#491 (5) { ["customer_name"]=> string(4) "User" ["profile_image"]=> string(47) "http://pet.huarisnaque.com/pet/upload/90113.png" ["jid"]=> string(18) "user128#canopus-pc" ["customer_id"]=> string(3) "128" ["phone"]=> string(10) "4784784784" } [1]=> object(stdClass)#494 (5) { ["customer_name"]=> string(6) "Khatru" ["profile_image"]=> string(46) "http://smartpetmanagement.com/upload/61694.png" ["jid"]=> string(20) "khatru321#canopus-pc" ["customer_id"]=> string(3) "321" ["phone"]=> string(10) "9686838386" } [2]=> object(stdClass)#495 (5) { ["customer_name"]=> string(5) "pppoo" ["profile_image"]=> string(46) "http://smartpetmanagement.com/upload/35210.png" ["jid"]=> string(17) "yyy329#canopus-pc" ["customer_id"]=> string(3) "329" ["phone"]=> string(10) "9525538835" } [3]=> object(stdClass)#496 (5) { ["customer_name"]=> string(7) "Xitxitx" ["profile_image"]=> NULL ["jid"]=> string(21) "xitxitx330#canopus-pc" ["customer_id"]=> string(3) "330" ["phone"]=> string(10) "6535383535" } [4]=> object(stdClass)#497 (5) { ["customer_name"]=> string(25) "The Following Document yf" ["profile_image"]=> string(46) "http://smartpetmanagement.com/upload/13712.png" ["jid"]=> string(39) "the following document yf589#canopus-pc" ["customer_id"]=> string(3) "589" ["phone"]=> string(10) "9535383535" } [5]=> object(stdClass)#498 (5) { ["customer_name"]=> string(3) "Pet" ["profile_image"]=> NULL ["jid"]=> string(17) "pet590#canopus-pc" ["customer_id"]=> string(3) "590" ["phone"]=> string(10) "6560530537" } [6]=> object(stdClass)#499 (5) { ["customer_name"]=> string(10) "Sanjay Pra" ["profile_image"]=> NULL ["jid"]=> string(24) "sanjay pra599#canopus-pc" ["customer_id"]=> string(3) "599" ["phone"]=> string(10) "2828282822" } }
there are two arrays,we need to remove duplicate records from the array containing two elements from result having elements from the customerlist.
here is my approach
for($i=0;$i<count($customerslist);$i++)
{
for($j=0;$j<count($result);$i++)
{
// if($result[$j]->block_to_id==$customerslist[$i]->customer_id)
{
unset($customerslist[$i]);
}
echo $result[$j]->block_to_id."<br/>";
}
}

You can unset the values after you find the indexes that has the duplicate. Because if you unset it inside the for loop of checking your duplicates it will produce this error because the size/count of the array changes and the index does not match anymore:
Notice: Undefined offset: 2
So my solution is you can put the matching indexes on another array then unset them on another for loop shown bellow:
$result=array(
array('id' => 64,
"block_from_id" => 117,
"block_to_id" => 329,
"block_status" => 0,
"username" => "pppoo"),
array("id"=> 65,
"block_from_id"=> 117,
"block_to_id"=> 590,
"block_status"=> 0,
"username"=> "Pet"
)
);
$customerlist= array(
array("customer_name" => "User" ,"profile_image" => "http://pet.huarisnaque.com/pet/upload/90113.png" ,"jid" => "user128#canopus-pc" ,"customer_id" => "128" ,"phone" => "4784784784"),
array("customer_name" => "Khatru" ,"profile_image" => "http://smartpetmanagement.com/upload/61694.png" ,"jid" => "khatru321#canopus-pc" ,"customer_id" => "321" ,"phone" => "9686838386"),
array("customer_name" => "pppoo" ,"profile_image" => "http://smartpetmanagement.com/upload/35210.png" ,"jid" => "yyy329#canopus-pc" ,"customer_id" => "329" ,"phone" => "9525538835"),
array("customer_name" => "Xitxitx" ,"profile_image " => NULL ,"jid" => "xitxitx330#canopus-pc" ,"customer_id" => "330" ,"phone" => "6535383535"),
array("customer_name" => "The Following Document yf" ,"profile_image" => "http://smartpetmanagement.com/upload/13712.png" ,"jid" => "the following document yf589#canopus-pc" ,"customer_id" => "589" ,"phone" => "9535383535"),
array("customer_name" => "Pet" ,"profile_image " => NULL ,"jid" => "pet590#canopus-pc" ,"customer_id" => "590" ,"phone" => "6560530537"),
array("customer_name" => "Sanjay Pra" ,"profile_image " => NULL ,"jid" => "sanjay pra599#canopus-pc" ,"customer_id" => "599" ,"phone" => "2828282822")
);
echo "Before:". sizeof($customerlist) ."<br>";
print_r($customerlist);
echo "<br>";
echo "<br>";
$match = array();
for ($i=0; $i < sizeof($result) ; $i++) {
for ($ii=0; $ii < sizeof($customerlist) ; $ii++) {
if ($result[$i]['block_to_id'] == $customerlist[$ii]['customer_id']) {
$match[] = $ii;
echo " Match INDEX on result $i == customerlist $ii<br>";
}
}
}
for ($i=0; $i < sizeof($match) ; $i++) {
$ii = $match[$i];
unset($customerlist[$ii]);
}
echo "<br>";
echo "After: ". sizeof($customerlist) ."<br>";
print_r($customerlist);
OUTPUT:
Before:7
Array ( [0] => Array ( [customer_name] => User [profile_image] => http://pet.huarisnaque.com/pet/upload/90113.png [jid] => user128#canopus-pc [customer_id] => 128 [phone] => 4784784784 ) [1] => Array ( [customer_name] => Khatru [profile_image] => http://smartpetmanagement.com/upload/61694.png [jid] => khatru321#canopus-pc [customer_id] => 321 [phone] => 9686838386 ) [2] => Array ( [customer_name] => pppoo [profile_image] => http://smartpetmanagement.com/upload/35210.png [jid] => yyy329#canopus-pc [customer_id] => 329 [phone] => 9525538835 ) [3] => Array ( [customer_name] => Xitxitx [profile_image ] => [jid] => xitxitx330#canopus-pc [customer_id] => 330 [phone] => 6535383535 ) [4] => Array ( [customer_name] => The Following Document yf [profile_image] => http://smartpetmanagement.com/upload/13712.png [jid] => the following document yf589#canopus-pc [customer_id] => 589 [phone] => 9535383535 ) [5] => Array ( [customer_name] => Pet [profile_image ] => [jid] => pet590#canopus-pc [customer_id] => 590 [phone] => 6560530537 ) [6] => Array ( [customer_name] => Sanjay Pra [profile_image ] => [jid] => sanjay pra599#canopus-pc [customer_id] => 599 [phone] => 2828282822 ) )
Match INDEX on result 0 == customerlist 2
Match INDEX on result 1 == customerlist 5
After: 5
Array ( [0] => Array ( [customer_name] => User [profile_image] => http://pet.huarisnaque.com/pet/upload/90113.png [jid] => user128#canopus-pc [customer_id] => 128 [phone] => 4784784784 ) [1] => Array ( [customer_name] => Khatru [profile_image] => http://smartpetmanagement.com/upload/61694.png [jid] => khatru321#canopus-pc [customer_id] => 321 [phone] => 9686838386 ) [3] => Array ( [customer_name] => Xitxitx [profile_image ] => [jid] => xitxitx330#canopus-pc [customer_id] => 330 [phone] => 6535383535 ) [4] => Array ( [customer_name] => The Following Document yf [profile_image] => http://smartpetmanagement.com/upload/13712.png [jid] => the following document yf589#canopus-pc [customer_id] => 589 [phone] => 9535383535 ) [6] => Array ( [customer_name] => Sanjay Pra [profile_image ] => [jid] => sanjay pra599#canopus-pc [customer_id] => 599 [phone] => 2828282822 ) )

Related

order by max value multidimension array/object

I need to order this array/object by max value desc grossConversions
from max to min
max 10 results
I have this output to order
array(19) {
[0] => object(stdClass) #355 (8) { ["campaign_name"]= > string(27)
"Scrub Viso Granuli Vegetali" ["campaign_id"] => int(14)["currency"] => string(3)
"EUR" ["approvedConversions"] => int(0)["pendingConversions"] => int(0)["grossConversions"] => int(0)["payout"] => int(0)["pendingPayout"] => int(0)
} [1] => object(stdClass) #356 (8) { ["campaign_name"]= > string(33)
"Crema Viso/collo Acido Jaluronico" ["campaign_id"] => int(16)["currency"] => string(3)
"EUR" ["approvedConversions"] => int(0)["pendingConversions"] => int(0)["grossConversions"] => int(0)["payout"] => int(0)["pendingPayout"] => int(0)
} [2] => object(stdClass) #357 (8) { ["campaign_name"]= > string(13)
"Home Training" ["campaign_id"] => int(5)["currency"] => string(3)
"EUR" ["approvedConversions"] => int(0)["pendingConversions"] => int(0)["grossConversions"] => int(0)["payout"] => int(0)["pendingPayout"] => int(0)
} [3] => object(stdClass) #358 (8) { ["campaign_name"]= > string(25)
"4x Libri di apprendimento" ["campaign_id"] => int(4)["currency"] => string(3)
"EUR" ["approvedConversions"] => int(1)["pendingConversions"] => int(0)["grossConversions"] => int(2)["payout"] => int(15)["pendingPayout"] => int(0)
} [4] => object(stdClass) #359 (8) { ["campaign_name"]= > string(26)
"Tin-UP - Abbronzante Spray" ["campaign_id"] => int(11)["currency"] => string(3)
"EUR" ["approvedConversions"] => int(0)["pendingConversions"] => int(0)["grossConversions"] => int(0)["payout"] => int(0)["pendingPayout"] => int(0)
} [5] => object(stdClass) #360 (8) { ["campaign_name"]= > string(23)
"Aspirapolvere Ciclonica" ["campaign_id"] => int(22)["currency"] => string(3)
"EUR" ["approvedConversions"] => int(0)["pendingConversions"] => int(0)["grossConversions"] => int(0)["payout"] => int(0)["pendingPayout"] => int(0)
} [6] => object(stdClass) #361 (8) { ["campaign_name"]= > string(15)
"Centurion Power" ["campaign_id"] => int(12)["currency"] => string(3)
"EUR" ["approvedConversions"] => int(0)["pendingConversions"] => int(0)["grossConversions"] => int(0)["payout"] => int(0)["pendingPayout"] => int(0)
} [7] => object(stdClass) #362 (8) { ["campaign_name"]= > string(27)
"Tonico Te' Bianco Te' Rosso" ["campaign_id"] => int(10)["currency"] => string(3)
"EUR" ["approvedConversions"] => int(0)["pendingConversions"] => int(0)["grossConversions"] => int(0)["payout"] => int(0)["pendingPayout"] => int(0)
} [8] => object(stdClass) #363 (8) { ["campaign_name"]= > string(10)
"Air Freeze" ["campaign_id"] => int(13)["currency"] => string(3)
"EUR" ["approvedConversions"] => int(0)["pendingConversions"] => int(1)["grossConversions"] => int(3)["payout"] => int(0)["pendingPayout"] => int(16)
} [9] => object(stdClass) #364 (8) { ["campaign_name"]= > string(17)
"Monopattino Urban" ["campaign_id"] => int(25)["currency"] => string(3)
"EUR" ["approvedConversions"] => int(0)["pendingConversions"] => int(0)["grossConversions"] => int(0)["payout"] => int(0)["pendingPayout"] => int(0)
}
}
how Can I order by max value grossConversions DESC?
I have try
function cmp($a, $b) {
if ($a['grossConversions'] == $b['grossConversions']) {
return 0;
}
return ($a['grossConversions'] < $b['grossConversions']) ? 1 : -1;
}
uasort($getReportCampaign->records, 'cmp');
but I get this error
Fatal error: Uncaught Error: Cannot use object of type stdClass as array in
All your lines of your array are standard object. I guess you have done json_decode
So instead of this
$a['grossConversions']
do that
$a->grossConversions
Or if you want to use array, do
json_decode(json_encode($my_array, true), true)
it will "convert" your stdClass as an associative array thanks to the true param of json_decode (cf docs)

Convert CSV to object array in PHP

Hi I am creating a function that pulls my uploaded CSV via ajax then I would like to form an json object that can be posted to the woocommerce API.
The first loop in the while loop creates my labels array and every loop through it is using the labels as a key.
if ($file !== FALSE) {
while (($data = fgetcsv($file, 0, ",")) !== FALSE):
// print_r($data);
if($row == 1) {
$labels = $data;
} else {
$new_data = array_combine($labels, $data);
array_push($csv_data, $new_data);
}
$row++;
endwhile;
foreach($csv_data as $data){
echo "New Customer: ";
print_r($data);
}
fclose($file);
}
When I run this code I get this result:
New Customer: Array
(
[Name] => Test Dude
[Username] => Test
[Email] => test#johndeer.com
[Orders] => 1
[proceed_id] => 765
[AOV] => 765
[Country / Region] => US
[City] => Testerton
[Region] => AR
[Postal Code] => 71601
[Address] => John Drive
)
New Customer: Array
(
[Name] => John Deer
[Username] => johndeer
[Email] => johndeer#jonnydeers.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => CA
[City] => Testerton
[Region] => AB
[Postal Code] => T6M 2V5
[Address] => John Street
)
New Customer: Array
(
[Name] => John Doe
[Username] => john.doe
[Email] => john.doe#example.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => US
[City] => San Francisco
[Region] => CA
[Postal Code] => 94103
[Address] => John Drive
)
New Customer: Array
(
[Name] => John Doe
[Username] => johndoe
[Email] => johndoe#example.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => US
[City] => San Francisco
[Region] => CA
[Postal Code] => 94103
[Address] =>
)
New Customer: Array
(
[Name] => John Doe
[Username] => johndoe1
[Email] => johndoe123#example.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => US
[City] => San Francisco
[Region] => CA
[Postal Code] => 94103
[Address] =>
)
New Customer: Array
(
[Name] => John Doe
[Username] => johndoe7
[Email] => johndoe1234#example.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => US
[City] => San Francisco
[Region] => CA
[Postal Code] => 94103
[Address] =>
)
New Customer: Array
(
[Name] => John Doe
[Username] => johndoe5
[Email] => johndoe5#example.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => US
[City] => San Francisco
[Region] => CA
[Postal Code] => 94103
[Address] =>
)
New Customer: Array
(
[Name] => John Doe
[Username] => johndoe6
[Email] => johndoe6#example.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => US
[City] => San Francisco
[Region] => CA
[Postal Code] => 94103
[Address] =>
)
New Customer: Array
(
[Name] => John Doe
[Username] => johndoe8
[Email] => johndoe8#example.com
[Orders] => 0
[proceed_id] => 0
[AOV] => 0
[Country / Region] => US
[City] => San Francisco
[Region] => CA
[Postal Code] => 94103
[Address] =>
)
0
It displays New Customer: in each loop just to show that is one loop cycle. However when I try to select "Name" or "proceed_id" it returns null. I've tried to use both of these syntaxes $data['Name] and $data->Name. How do I select by key inside this foreach?
foreach($csv_data as $data){
//$data is the individual rows
$proceed_id = $data['proceed_id'];
}
EDIT 1:
Here is the vardump of $csv_data outside loop.
array(9) {
[0]=>
array(11) {
["Name"]=>
string(9) "Test Dude"
["Username"]=>
string(4) "Test"
["Email"]=>
string(17) "test#johndeer.com"
["Orders"]=>
string(1) "1"
["proceed_id"]=>
string(3) "765"
["AOV"]=>
string(3) "765"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(9) "Testerton"
["Region"]=>
string(2) "AR"
["Postal Code"]=>
string(5) "71601"
["Address"]=>
string(10) "John Drive"
}
[1]=>
array(11) {
["Name"]=>
string(9) "John Deer"
["Username"]=>
string(8) "johndeer"
["Email"]=>
string(23) "johndeer#jonnydeers.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "CA"
["City"]=>
string(9) "Testerton"
["Region"]=>
string(2) "AB"
["Postal Code"]=>
string(7) "T6M 2V5"
["Address"]=>
string(11) "John Street"
}
[2]=>
array(11) {
["Name"]=>
string(8) "John Doe"
["Username"]=>
string(8) "john.doe"
["Email"]=>
string(20) "john.doe#example.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(13) "San Francisco"
["Region"]=>
string(2) "CA"
["Postal Code"]=>
string(5) "94103"
["Address"]=>
string(10) "John Drive"
}
[3]=>
array(11) {
["Name"]=>
string(8) "John Doe"
["Username"]=>
string(7) "johndoe"
["Email"]=>
string(19) "johndoe#example.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(13) "San Francisco"
["Region"]=>
string(2) "CA"
["Postal Code"]=>
string(5) "94103"
["Address"]=>
string(0) ""
}
[4]=>
array(11) {
["Name"]=>
string(8) "John Doe"
["Username"]=>
string(8) "johndoe1"
["Email"]=>
string(22) "johndoe123#example.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(13) "San Francisco"
["Region"]=>
string(2) "CA"
["Postal Code"]=>
string(5) "94103"
["Address"]=>
string(0) ""
}
[5]=>
array(11) {
["Name"]=>
string(8) "John Doe"
["Username"]=>
string(8) "johndoe7"
["Email"]=>
string(23) "johndoe1234#example.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(13) "San Francisco"
["Region"]=>
string(2) "CA"
["Postal Code"]=>
string(5) "94103"
["Address"]=>
string(0) ""
}
[6]=>
array(11) {
["Name"]=>
string(8) "John Doe"
["Username"]=>
string(8) "johndoe5"
["Email"]=>
string(20) "johndoe5#example.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(13) "San Francisco"
["Region"]=>
string(2) "CA"
["Postal Code"]=>
string(5) "94103"
["Address"]=>
string(0) ""
}
[7]=>
array(11) {
["Name"]=>
string(8) "John Doe"
["Username"]=>
string(8) "johndoe6"
["Email"]=>
string(20) "johndoe6#example.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(13) "San Francisco"
["Region"]=>
string(2) "CA"
["Postal Code"]=>
string(5) "94103"
["Address"]=>
string(0) ""
}
[8]=>
array(11) {
["Name"]=>
string(8) "John Doe"
["Username"]=>
string(8) "johndoe8"
["Email"]=>
string(20) "johndoe8#example.com"
["Orders"]=>
string(1) "0"
["proceed_id"]=>
string(1) "0"
["AOV"]=>
string(1) "0"
["Country / Region"]=>
string(2) "US"
["City"]=>
string(13) "San Francisco"
["Region"]=>
string(2) "CA"
["Postal Code"]=>
string(5) "94103"
["Address"]=>
string(0) ""
}
}
0
Var_export() =
array (
0 =>
array (
'Name' => 'Test Dude',
'Username' => 'Test',
'Email' => 'test#johndeer.com',
'Orders' => '1',
'proceed_id' => '765',
'AOV' => '765',
'Country / Region' => 'US',
'City' => 'Testerton',
'Region' => 'AR',
'Postal Code' => '71601',
'Address' => 'John Drive',
),
1 =>
array (
'Name' => 'John Deer',
'Username' => 'johndeer',
'Email' => 'johndeer#jonnydeers.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'CA',
'City' => 'Testerton',
'Region' => 'AB',
'Postal Code' => 'T6M 2V5',
'Address' => 'John Street',
),
2 =>
array (
'Name' => 'John Doe',
'Username' => 'john.doe',
'Email' => 'john.doe#example.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'US',
'City' => 'San Francisco',
'Region' => 'CA',
'Postal Code' => '94103',
'Address' => 'John Drive',
),
3 =>
array (
'Name' => 'John Doe',
'Username' => 'johndoe',
'Email' => 'johndoe#example.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'US',
'City' => 'San Francisco',
'Region' => 'CA',
'Postal Code' => '94103',
'Address' => '',
),
4 =>
array (
'Name' => 'John Doe',
'Username' => 'johndoe1',
'Email' => 'johndoe123#example.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'US',
'City' => 'San Francisco',
'Region' => 'CA',
'Postal Code' => '94103',
'Address' => '',
),
5 =>
array (
'Name' => 'John Doe',
'Username' => 'johndoe7',
'Email' => 'johndoe1234#example.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'US',
'City' => 'San Francisco',
'Region' => 'CA',
'Postal Code' => '94103',
'Address' => '',
),
6 =>
array (
'Name' => 'John Doe',
'Username' => 'johndoe5',
'Email' => 'johndoe5#example.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'US',
'City' => 'San Francisco',
'Region' => 'CA',
'Postal Code' => '94103',
'Address' => '',
),
7 =>
array (
'Name' => 'John Doe',
'Username' => 'johndoe6',
'Email' => 'johndoe6#example.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'US',
'City' => 'San Francisco',
'Region' => 'CA',
'Postal Code' => '94103',
'Address' => '',
),
8 =>
array (
'Name' => 'John Doe',
'Username' => 'johndoe8',
'Email' => 'johndoe8#example.com',
'Orders' => '0',
'proceed_id' => '0',
'AOV' => '0',
'Country / Region' => 'US',
'City' => 'San Francisco',
'Region' => 'CA',
'Postal Code' => '94103',
'Address' => '',
),
)

Why my array does not sort correctly

I have two arrays $men, $sub , I want want to sort them with this condition: if($men["titre"] == $sub["menuParent"]) I put the content of the second array that contains $sub["menuParent"] in the first one.
Case:
//Array1 ($menu)
array(3) {
["menu1"] => array(3) {
["titre"] => string(6) "Title2"
["lien"] => string(0) ""
["order"] => string(1) "2"
}
["menu2"] => array(3) {
["titre"] => string(6) "Title3"
["lien"] => string(10) "google.com"
["order"] => string(1) "3"
}
["menu3"] => array(3) {
["titre"] => string(6) "Title1"
["lien"] => string(0) ""
["order"] => string(1) "1"
}
}
//Array2 ($submenu)
array(3) {
["submenu1"] => array(3) {
["titre"] => string(9) "SubTitle2"
["menuParent"] => string(6) "Title2"
["order"] => string(1) "2"
}
["submenu2"] => array(3) {
["titre"] => string(9) "SubTitle3"
["menuParent"] => string(6) "Title2"
["order"] => string(1) "3"
}
["submenu3"] => array(3) {
["titre"] => string(9) "SubTitle1"
["menuParent"] => string(6) "Title1"
["order"] => string(1) "1"
}
}
Here for exemple in:
array1 ["menu1"]["titre"] => string(6) "Title2"
array2 ["submenu1"]["menuParent"] => string(6) "Title2"
Now while ["menu1"]["titre"] & ["submenu1"]["menuParent"] values are the same "Title2", So I push :
["submenu1"] => array(3) {
["titre"] => string(9) "SubTitle2"
["menuParent"] => string(6) "Title2"
["order"] => string(1) "2"
}
in:
["menu1"] => array(3) {
["titre"] => string(6) "Title2"
["lien"] => string(0) ""
["order"] => string(1) "2"
}
as child like this:
array(3) {
["menu1"] => array(3) {
["titre"] => string(6) "Title2"
["lien"] => string(0) ""
["order"] => string(1) "2"
["child"] => array(3) {
[0] => array(3) {
["titre"] => string(9) "SubTitle2"
["menuParent"] => string(6) "Title2"
["order"] => string(1) "2"
}
}
So I do all this for whole elements`
Here is my try, but It push all the elements in ["menu2"] instead of ["submenu1"], ["submenu2"] IN ["menu1"] and ["submenu3"] IN ["menu3"]
My Try:
/*Injection submenu to Menu*/
foreach($menu as $men) {
foreach($submenu as $sub) {
if($men["titre"] == $sub["menuParent"]){
$key = key($menu);
$menu[$key]['child'][] = array($sub["titre"], $sub["menuParent"], $sub["order"]);
//My array_push doesn't work also
array_push($menu, array($sub["titre"], $sub["menuParent"], $sub["order"]))
}
}
}
Result:
array(3) {
["menu1"] => array(3) {
["titre"] => string(6) "Title2"
["lien"] => string(0) ""
["order"] => string(1) "2"
}
["menu2"] => array(4) {
["titre"] => string(6) "Title3"
["lien"] => string(10) "google.com"
["order"] => string(1) "3"
["child"] => array(3) {
[0] => array(3) {
[0] => string(9) "SubTitle2"
[1] => string(6) "Title2"
[2] => string(1) "2"
}
[1] => array(3) {
[0] => string(9) "SubTitle3"
[1] => string(6) "Title2"
[2] => string(1) "3"
}
[2] => array(3) {
[0] => string(9) "SubTitle1"
[1] => string(6) "Title1"
[2] => string(1) "1"
}
}
}
["menu3"] => array(3) {
["titre"] => string(6) "Title1"
["lien"] => string(0) ""
["order"] => string(1) "1"
}
}
Where I'm at fault, If anyone can explain to me why and if I took the good way.
The problem is where your picking the key up, you can use the foreach ( as $key->$value) method to get the key of each item (foreach works on it's own copy of the array and so picking the key from the original array isn't the same as the current one). Also you could use &$men, the & allows you to alter the original array element rather than create a new entry and trying to push the new element into the array...
foreach($menu as $key => $men) {
foreach($submenu as $key1=>$sub) {
if($men["titre"] == $sub["menuParent"]){
$menu[$key]['child'][$key1] = array($sub["titre"], $sub["menuParent"], $sub["order"]);
}
}
}
gives...
Array
(
[menu1] => Array
(
[titre] => Title2
[lien] =>
[order] => 2
[child] => Array
(
[submenu1] => Array
(
[0] => SubTitle2
[1] => Title2
[2] => 2
)
[submenu2] => Array
(
[0] => SubTitle3
[1] => Title2
[2] => 3
)
)
)
[menu2] => Array
(
[titre] => Title3
[lien] => google.com
[order] => 3
)
[menu3] => Array
(
[titre] => Title1
[lien] =>
[order] => 1
[child] => Array
(
[submenu3] => Array
(
[0] => SubTitle1
[1] => Title1
[2] => 1
)
)
)
)
Update:
Code using &$men, note that in setting the data it no longer uses the key, but uses the $men variable.
foreach($menu as &$men) {
foreach($submenu as $key=>$sub) {
if($men["titre"] == $sub["menuParent"]){
$men['child'][$key] = array($sub["titre"], $sub["menuParent"], $sub["order"]);
}
}
}
unset($men); // Good practice to ensure reference is reset

PHP preg_match iteration

I have an
array() {
[0] =>
string(1) "id1"
[1] =>
string(14) "Gucci"
[2] =>
string(1) "id1"
[3] =>
string(24) "bag1"
[4] =>
string(4) "id2"
[5] =>
string(14) "Gucci"
[6] =>
string(4) "id2"
[7] =>
string(25) "Gucci bag2"
[8] =>
string(4) "id3"
[9] =>
string(14) "Gucci"
[10] =>
string(4) "id3"
[11] =>
string(27) "bag3"
..more
the key 0, 2, 4, ...are the id
the key 1, 5 .. are the brand
the key 3, 6 are the name
i want to loop trough the array to check if $array[1] is in $array[3], and then $array[5] is in $array[7]
how can i have the iteration of
if (preg_match("/$array[$i+1]/i", "$array[$i+3]")) {
echo "$array[$i]";
} else {
echo $array[$i+1] . ' ' . $array[$i+3];
}
Thank you

array key value pair at a specified point in array

I have an associative array , i would like to add some more key and values
Array
(
[0] => Array
(
[NUMBER] => 67
[TYPE] => Other
[DATE] => 3/31/2011
)
[1] => Array
(
[NUMBER] => 87
[TYPE] => something
[DATE] => 3/28/2011
)
[2] => Array
(
[NUMBER] => 67
[TYPE] => Other
[DATE] => 3/2/2011
)
)
In Above array i want to add another key named STATUS and value before DATE
so that finally iget
Array
(
[0] => Array
(
[NUMBER] => 67
[TYPE] => Other
[STATUS] => waiting
[DATE] => 3/31/2011
)
}
canPlease give me proper direction
$arr = Array(
0 => Array('NUMBER' => 67, 'TYPE' => Other, 'DATE' => '3/32/2011'),
1 => Array('NUMBER' => 87, 'TYPE' => something, 'DATE' => '3/28/2011'),
2 => Array('NUMBER' => 67, 'TYPE' => Other, 'DATE' => '3/2/2011')
);
foreach($arr as $key => $value) {
$arr[$key] = array_slice($value, 0, 2) +
array('Status' => 'waiting') +
array_slice($value, -1);
}
var_dump($arr);
gives the following array:
array(3) {
[0]=>
array(4) {
["NUMBER"]=>
int(67)
["TYPE"]=>
string(5) "Other"
["Status"]=>
string(7) "waiting"
["DATE"]=>
string(9) "3/32/2011"
}
[1]=>
array(4) {
["NUMBER"]=>
int(87)
["TYPE"]=>
string(9) "something"
["Status"]=>
string(7) "waiting"
["DATE"]=>
string(9) "3/28/2011"
}
[2]=>
array(4) {
["NUMBER"]=>
int(67)
["TYPE"]=>
string(5) "Other"
["Status"]=>
string(7) "waiting"
["DATE"]=>
string(8) "3/2/2011"
}
}

Categories