I want to sort all php arrays based on first main array.
This is the main array which I want to use to sort all other arrays:
Array (
[0] => 10
[1] => 14
[2] => 15
[3] => 20
[4] => 21
[5] => 24
[6] => 25
[7] => 28
[8] => 30
[9] => 45
[10] => 60
[11] => 90
[12] => 120
[13] => 150
[14] => 180
[15] => 210
[16] => 240
[17] => 270
[18] => 365
)
This are arrays which need to be sorted :
Array (
[0] => Array
(
[14] => 49.21
[20] => 71.04
[25] => 89.58
[30] => 100.00
)
[1] => Array
(
[180] => 412.00
[150] => 347.00
[120] => 285.00
[90] => 224.00
[60] => 165.00
[30] => 100.00
[14] => 47.00
)
)
I need that final result look like this:
Array (
[0] => Array
(
[10] => n/a
[14] => 49.21
[15] => n/a
[20] => 71.04
[21] => n/a
[24] => n/a
[25] => 89.58
[28] => n/a
[30] => 100.00
[45] => n/a
[60] => n/a
[90] => n/a
[120] => n/a
[150] => n/a
[180] => n/a
[210] => n/a
[240] => n/a
[270] => n/a
[365] => n/a
)
[1] => Array
(
[10] => n/a
[14] => 71.04
[15] => n/a
[20] => n/a
[21] => n/a
[24] => n/a
[25] => n/a
[28] => n/a
[30] => 100.00
[45] => n/a
[60] => 165.00
[90] => 224.00
[120] => 285.00
[150] => 347.00
[180] => 412.00
[210] => n/a
[240] => n/a
[270] => n/a
[365] => n/a
)
)
Thanks.
Assuming your initial array is $source, and $todo is your second array with the two subsets, then:
$keys = array_flip($todo);
$keys = array_map(function() { return 'n/a'; }, $keys); // set all values to be "n/a";
foreach($todo as $idx => $do) {
$todo[$idx] = $do + $keys; // merge without renumbering.
}
If I understand it right from your question, you want to make both arrays of the same length and set keys not in them to "n/a".
Variant with array_merge (Demo):
$shorten = array(
0 => 10,
1 => 14,
2 => 15,
3 => 20,
4 => 21,
5 => 24,
6 => 25,
7 => 28,
8 => 30,
9 => 45,
10 => 60,
11 => 90,
12 => 120,
13 => 150,
14 => 180,
15 => 210,
16 => 240,
17 => 270,
18 => 365,
);
$data = array(
0 => array(
14 => '49.21',
20 => '71.04',
25 => '89.58',
30 => '100.00',
),
1 => array(
180 => '412.00',
150 => '347.00',
120 => '285.00',
90 => '224.00',
60 => '165.00',
30 => '100.00',
14 => '47.00',
),
);
// default array as the base
$shorten = array_combine($shorten, array_fill(0, count($shorten), 'n/a'));
foreach($data as &$array) {
// merge to get set members
$array = array_merge($shorten, $array);
}
unset($array);
var_dump($data);
Result:
array(2) {
[0]=>
array(23) {
[0]=>
string(3) "n/a"
[1]=>
string(3) "n/a"
[2]=>
string(3) "n/a"
[3]=>
string(3) "n/a"
[4]=>
string(3) "n/a"
[5]=>
string(3) "n/a"
[6]=>
string(3) "n/a"
[7]=>
string(3) "n/a"
[8]=>
string(3) "n/a"
[9]=>
string(3) "n/a"
[10]=>
string(3) "n/a"
[11]=>
string(3) "n/a"
[12]=>
string(3) "n/a"
[13]=>
string(3) "n/a"
[14]=>
string(3) "n/a"
[15]=>
string(3) "n/a"
[16]=>
string(3) "n/a"
[17]=>
string(3) "n/a"
[18]=>
string(3) "n/a"
[19]=>
string(5) "49.21"
[20]=>
string(5) "71.04"
[21]=>
string(5) "89.58"
[22]=>
string(6) "100.00"
}
[1]=>
array(26) {
[0]=>
string(3) "n/a"
[1]=>
string(3) "n/a"
[2]=>
string(3) "n/a"
[3]=>
string(3) "n/a"
[4]=>
string(3) "n/a"
[5]=>
string(3) "n/a"
[6]=>
string(3) "n/a"
[7]=>
string(3) "n/a"
[8]=>
string(3) "n/a"
[9]=>
string(3) "n/a"
[10]=>
string(3) "n/a"
[11]=>
string(3) "n/a"
[12]=>
string(3) "n/a"
[13]=>
string(3) "n/a"
[14]=>
string(3) "n/a"
[15]=>
string(3) "n/a"
[16]=>
string(3) "n/a"
[17]=>
string(3) "n/a"
[18]=>
string(3) "n/a"
[19]=>
string(6) "412.00"
[20]=>
string(6) "347.00"
[21]=>
string(6) "285.00"
[22]=>
string(6) "224.00"
[23]=>
string(6) "165.00"
[24]=>
string(6) "100.00"
[25]=>
string(5) "47.00"
}
}
Variant with mapping function (Demo):
$shorten = array(
0 => 10,
1 => 14,
2 => 15,
3 => 20,
4 => 21,
5 => 24,
6 => 25,
7 => 28,
8 => 30,
9 => 45,
10 => 60,
11 => 90,
12 => 120,
13 => 150,
14 => 180,
15 => 210,
16 => 240,
17 => 270,
18 => 365,
);
// overload $shorten array with a mapping function
$shorten = function(array $a) use ($shorten)
{
$r = array();
foreach($shorten as $i => $k)
{
$r[$k] = isset($a[$k]) ? $a[$k] : 'n/a';
}
return $r;
};
$data = array(
0 => array(
14 => '49.21',
20 => '71.04',
25 => '89.58',
30 => '100.00',
),
1 => array(
180 => '412.00',
150 => '347.00',
120 => '285.00',
90 => '224.00',
60 => '165.00',
30 => '100.00',
14 => '47.00',
),
);
// apply mapping function to $data
$data = array_map($shorten, $data);
var_dump($data); # result
Result:
array(2) {
[0]=>
array(19) {
[10]=>
string(3) "n/a"
[14]=>
string(5) "49.21"
[15]=>
string(3) "n/a"
[20]=>
string(5) "71.04"
[21]=>
string(3) "n/a"
[24]=>
string(3) "n/a"
[25]=>
string(5) "89.58"
[28]=>
string(3) "n/a"
[30]=>
string(6) "100.00"
[45]=>
string(3) "n/a"
[60]=>
string(3) "n/a"
[90]=>
string(3) "n/a"
[120]=>
string(3) "n/a"
[150]=>
string(3) "n/a"
[180]=>
string(3) "n/a"
[210]=>
string(3) "n/a"
[240]=>
string(3) "n/a"
[270]=>
string(3) "n/a"
[365]=>
string(3) "n/a"
}
[1]=>
array(19) {
[10]=>
string(3) "n/a"
[14]=>
string(5) "47.00"
[15]=>
string(3) "n/a"
[20]=>
string(3) "n/a"
[21]=>
string(3) "n/a"
[24]=>
string(3) "n/a"
[25]=>
string(3) "n/a"
[28]=>
string(3) "n/a"
[30]=>
string(6) "100.00"
[45]=>
string(3) "n/a"
[60]=>
string(6) "165.00"
[90]=>
string(6) "224.00"
[120]=>
string(6) "285.00"
[150]=>
string(6) "347.00"
[180]=>
string(6) "412.00"
[210]=>
string(3) "n/a"
[240]=>
string(3) "n/a"
[270]=>
string(3) "n/a"
[365]=>
string(3) "n/a"
}
}
Related
$db_a = new PDO("odbc:MYOB",'user','pass');
foreach($db_a->query("SELECT * FROM Sale WHERE SaleID = 108605") as $row) {
echo '<pre>';
print_r($row);
echo '</pre>';
}
This returns all the table column titles as well as the first five results but everything after that is blank. I've tried issuing the same select statement through MS access and it works correctly
Array
(
[ItemSaleLineID] => 1605696
[0] => 1605696
[SaleLineID] => 1605696
[1] => 1605696
[SaleID] => 108605
[2] => 108605
[LineNumber] => 1
[3] => 1
[LineTypeID] => D
[4] => D
[Description] =>
[5] =>
[TaxExclusiveTotal] =>
[6] =>
[TaxInclusiveTotal] =>
[7] =>
[TaxCodeID] =>
[8] =>
[JobID] =>
[9] =>
[IsMultipleJob] =>
[10] =>
[Quantity] =>
[11] =>
[ItemID] =>
[12] =>
[TaxExclusiveUnitPrice] =>
[13] =>
[TaxInclusiveUnitPrice] =>
[14] =>
[Discount] =>
[15] =>
[CostOfGoodsSoldAmount] =>
[16] =>
[LocationID] =>
[17] =>
[Kit/ComponentIndicator] =>
[18] =>
)
Output from var_dump:
array(20) { ["SaleLineID"]=> string(7) "1605696" [0]=> string(7) "1605696" ["SaleID"]=> string(6) "108605" [1]=> string(6) "108605" ["LineNumber"]=> string(1) "1" [2]=> string(1) "1" ["LineTypeID"]=> string(1) "D" [3]=> string(1) "D" ["Description"]=> NULL [4]=> NULL ["TaxExclusiveAmount"]=> NULL [5]=> NULL ["TaxInclusiveAmount"]=> NULL [6]=> NULL ["TaxCodeID"]=> NULL [7]=> NULL ["JobID"]=> NULL [8]=> NULL ["IsMultipleJob"]=> NULL [9]=> NULL } array(20) { ["SaleLineID"]=> string(7) "1605697" [0]=> string(7) "1605697" ["SaleID"]=> string(6) "108605" [1]=> string(6) "108605" ["LineNumber"]=> string(1) "2" [2]=> string(1) "2" ["LineTypeID"]=> string(1) "D" [3]=> string(1) "D" ["Description"]=> NULL [4]=> NULL ["TaxExclusiveAmount"]=> NULL [5]=> NULL ["TaxInclusiveAmount"]=> NULL [6]=> NULL ["TaxCodeID"]=> NULL [7]=> NULL ["JobID"]=> NULL [8]=> NULL ["IsMultipleJob"]=> NULL [9]=> NULL } array(20) { ["SaleLineID"]=> string(7) "1605698" [0]=> string(7) "1605698" ["SaleID"]=> string(6) "108605" [1]=> string(6) "108605" ["LineNumber"]=> string(1) "3" [2]=> string(1) "3" ["LineTypeID"]=> string(1) "D" [3]=> string(1) "D" ["Description"]=> NULL [4]=> NULL ["TaxExclusiveAmount"]=> NULL [5]=> NULL ["TaxInclusiveAmount"]=> NULL [6]=> NULL ["TaxCodeID"]=> NULL [7]=> NULL ["JobID"]=> NULL [8]=> NULL ["IsMultipleJob"]=> NULL [9]=> NULL }
I'm working on a project with an API.
I gather data from a form and send this to their website.
The data is gathered through an array and then encoded to json format:
$pers_payload = array(
'gender' => 'Unknown', //or Male / Female
'first_name' => $_POST['billing_first_name'],
'family_name' => $_POST ['billing_last_name'],
'email' => $_POST['billing_email'],
'linked_as_contact_to_organization' => array(
array(
'organization_id' => $organization_id, // add the person as a contact to the newly created organization
'work_email' => $_POST['billing_email'],
'work_phone' => $_POST['billing_phone']
)
),
'visiting_address' => array(
'country_code' => 'NL'
), // can be extented with other address data
'postal_address' => array(
'country_code' => $_POST['billing_country']
) // can be extented with other address data
);
Then the post request.
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
}
I can also do a get request, this get request returns a multi dimensional array:
$tet = $SimplicateApi->makeApiCall('GET','/crm/person?q[first_name]=Kevin1');
If i do a var_dump on $tet i get this:
array(3) {
["data"] => array(2) {
[0] => array(11) {
["id"] => string(39)
"person:067af3bd2045824e62ac579e634623b8" ["interests"] => array(1) {
[0] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f278f47e6e9d48b8" ["name"] => string(19)
"Actief in Duitsland"
}
}["simplicate_url"] => string(51)
"https://emark.simplicate.nl/crm/person/view?id=3552" ["avatar"] => array(2) {
["initials"] => string(2)
"Kt" ["color"] => string(7)
"#03e084"
}["linked_as_contact_to_organization"] => array(1) {
[0] => array(7) {
["id"] => string(46)
"contactperson:0f16f418f1845749c79bebf9e1e753e5" ["organization_id"] => string(45)
"organization:8632b86ba41637262e0871767f96f43e" ["name"] => string(9)
"testing12" ["work_email"] => string(24)
"ma#e-marketingsupport.nl" ["work_phone"] => string(8)
"06269684" ["work_mobile"] => string(8)
"06269684" ["interests"] => array(16) {
[0] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:456e8b19c0079647" ["name"] => string(11)
"Twinkle 100"
}[1] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:a70e69b83382e85a" ["name"] => string(17)
"Bekend merk in NL"
}[2] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:ce50f1b5593ac180" ["name"] => string(15)
"Cross Border 30"
}[3] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f40eca1b281969d6" ["name"] => string(20)
"Meerdere vestigingen"
}[4] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:7435d7409a07cefb" ["name"] => string(26)
"Meer dan 100k in Duitsland"
}[5] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:cc072cea856ea23a" ["name"] => string(17)
"B2B leadgeneratie"
}[6] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f278f47e6e9d48b8" ["name"] => string(19)
"Actief in Duitsland"
}[7] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:9bbeb23d17283595" ["name"] => string(10)
"Exporteert"
}[8] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:97ed988af66b1abc" ["name"] => string(8)
"Debiteur"
}[9] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:3e31ffca2394bc38e1bb3149bee8b668" ["name"] => string(9)
"Marketing"
}[10] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:706fa5fa92c56081e1bb3149bee8b668" ["name"] => string(6)
"Amazon"
}[11] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:05f1a5da1c4c7df2e1bb3149bee8b668" ["name"] => string(3)
"Jur"
}[12] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:477554ee16a0c738e1bb3149bee8b668" ["name"] => string(11)
"Vertalingen"
}[13] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:f5f1b7512245a3b5e1bb3149bee8b668" ["name"] => string(5)
"Adres"
}[14] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:d1123dfaa0073c82e1bb3149bee8b668" ["name"] => string(4)
"GmbH"
}[15] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:7d3458131ea89afbe1bb3149bee8b668" ["name"] => string(3)
"Web"
}
}
}
}["gender"] => string(7)
"Unknown" ["first_name"] => string(6)
"Kevin1" ["family_name"] => string(7)
"testing" ["full_name"] => string(14)
"Kevin1 testing" ["email"] => string(24)
"ma#e-marketingsupport.nl" ["phone"] => string(8)
"06269684"
}[1] => array(11) {
["id"] => string(39)
"person:067af3bd2045824ea8c16e7ea0baf9d6" ["interests"] => array(1) {
[0] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f278f47e6e9d48b8" ["name"] => string(19)
"Actief in Duitsland"
}
}["simplicate_url"] => string(51)
"https://emark.simplicate.nl/crm/person/view?id=3553" ["avatar"] => array(2) {
["initials"] => string(2)
"Kt" ["color"] => string(7)
"#dce1f3"
}["linked_as_contact_to_organization"] => array(1) {
[0] => array(7) {
["id"] => string(46)
"contactperson:f48fdcaaff0211e728a2e4ccf197900b" ["organization_id"] => string(45)
"organization:8632b86ba41637262e0871767f96f43e" ["name"] => string(9)
"testing12" ["work_email"] => string(24)
"ma#e-marketingsupport.nl" ["work_phone"] => string(8)
"06269684" ["work_mobile"] => string(8)
"06269684" ["interests"] => array(16) {
[0] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:456e8b19c0079647" ["name"] => string(11)
"Twinkle 100"
}[1] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:a70e69b83382e85a" ["name"] => string(17)
"Bekend merk in NL"
}[2] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:ce50f1b5593ac180" ["name"] => string(15)
"Cross Border 30"
}[3] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f40eca1b281969d6" ["name"] => string(20)
"Meerdere vestigingen"
}[4] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:7435d7409a07cefb" ["name"] => string(26)
"Meer dan 100k in Duitsland"
}[5] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:cc072cea856ea23a" ["name"] => string(17)
"B2B leadgeneratie"
}[6] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:f278f47e6e9d48b8" ["name"] => string(19)
"Actief in Duitsland"
}[7] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:9bbeb23d17283595" ["name"] => string(10)
"Exporteert"
}[8] => array(3) {
["value"] => bool(false)["id"] => string(25)
"interest:97ed988af66b1abc" ["name"] => string(8)
"Debiteur"
}[9] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:3e31ffca2394bc38e1bb3149bee8b668" ["name"] => string(9)
"Marketing"
}[10] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:706fa5fa92c56081e1bb3149bee8b668" ["name"] => string(6)
"Amazon"
}[11] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:05f1a5da1c4c7df2e1bb3149bee8b668" ["name"] => string(3)
"Jur"
}[12] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:477554ee16a0c738e1bb3149bee8b668" ["name"] => string(11)
"Vertalingen"
}[13] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:f5f1b7512245a3b5e1bb3149bee8b668" ["name"] => string(5)
"Adres"
}[14] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:d1123dfaa0073c82e1bb3149bee8b668" ["name"] => string(4)
"GmbH"
}[15] => array(3) {
["value"] => bool(false)["id"] => string(41)
"interest:7d3458131ea89afbe1bb3149bee8b668" ["name"] => string(3)
"Web"
}
}
}
}["gender"] => string(7)
"Unknown" ["first_name"] => string(6)
"Kevin1" ["family_name"] => string(7)
"testing" ["full_name"] => string(14)
"Kevin1 testing" ["email"] => string(24)
"ma#e-marketingsupport.nl" ["phone"] => string(8)
"06269684"
}
}["errors"] => NULL["debug"] => NULL
}
I dont want double values. For example if 'first_name' => $_POST['billing_first_name'],(in the $pers_payload variable) exists in $tet as a value. then dont run
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
}
else run:
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
}
I tried this from another question i asked:
foreach (array_keys($tet['data']) as $key) {
if (array_key_exists('first_name', $tet['data'][$key])
&& (strcasecmp($tet['data'][$key]['first_name'], 'Kevin1') == 0) ) {
echo "found key 'first_name' with value '" . $tet['data'][$key]['first_name'] . "'\n";
} else {
// perform your post request
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
}
}
This doesn't run the post request at all even if the value of first_name doesnt exist
I found the solution.
I accomplished this with a simple if statement:
if(strtolower($_POST['billing_last_name']) == strtolower($getOrg['data']['0']['linked_persons_contacts']['0']['family_name'])) {
// do nothing
} else {
$person = $SimplicateApi->makeApiCall('POST','/crm/person',json_encode($pers_payload));
}
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 ) )
I have a multidimensional php array that stores the opening and closing times of my shop. The array contains other data about the shop to however in this particular function I am trying to create a new array with only the day as an index value and the open and close hours as an array. Like the following
$hours = array(
'mon' => array('11:00-23:00'),
'tue' => array('11:00-23:00'),
'wed' => array('11:00-22:30'),
'thu' => array('11:00-02:30'),
'fri' => array('11:00-02:30'),
'sat' => array('11:00-02:30'),
'sun' => '',
);
My current foreach with a for loop goes through my existing array and returns a variable for each day, open and close time. However I have included just a simple print at the bottom for debugging purposes. Everytime the foreach returns the correct day: open to close however it always returns one extra :to as if it's doing an extra loop. I have dumped the original array and every time it returns an array with (7) as the count. Secondly what is the most efficient way to return an array like the one above from within this foreach for loop.
foreach ($this -> entries as $entry) {
for ($i = 0; $i < count($entry); $i++) {
$day = $entry[$i]['day_open']['value'];
$open = $entry[$i]['open_hour']['value'];
$close = $entry[$i]['close_hour']['value'];
print $day . " : " . $open . " to " . $close . "<br>";
}
}
Here is the var_dump of $this -> entries
array(3) { ["entries"]=> array(7) {
[0]=> array(11) { ["id"]=> string(1) "1" ["created"]=> int(1418692508) ["updated"]=> bool(false) ["created_by"]=> string(1) "1" ["ordering_count"]=> string(1) "1" ["close_hour"]=> array(3) { ["key"]=> string(4) "10pm" ["val"]=> string(8) "10:00 PM" ["value"]=> string(8) "10:00 PM" } ["open_hour"]=> array(3) { ["key"]=> string(3) "2pm" ["val"]=> string(8) "02:00 PM" ["value"]=> string(8) "02:00 PM" } ["day_open"]=> array(3) { ["key"]=> string(3) "Sun" ["val"]=> string(3) "Sun" ["value"]=> string(3) "Sun" } ["last"]=> string(1) "0" ["odd_even"]=> string(3) "odd" ["count"]=> int(1) }
[1]=> array(11) { ["id"]=> string(1) "2" ["created"]=> int(1418692519) ["updated"]=> int(1418698391) ["created_by"]=> string(1) "1" ["ordering_count"]=> string(1) "2" ["close_hour"]=> array(3) { ["key"]=> string(4) "10pm" ["val"]=> string(8) "10:00 PM" ["value"]=> string(8) "10:00 PM" } ["open_hour"]=> array(3) { ["key"]=> string(3) "2pm" ["val"]=> string(8) "02:00 PM" ["value"]=> string(8) "02:00 PM" } ["day_open"]=> array(3) { ["key"]=> string(3) "Mon" ["val"]=> string(3) "Mon" ["value"]=> string(3) "Mon" } ["last"]=> string(1) "0" ["odd_even"]=> string(4) "even" ["count"]=> int(2) }
[2]=> array(11) { ["id"]=> string(1) "3" ["created"]=> int(1418692525) ["updated"]=> bool(false) ["created_by"]=> string(1) "1" ["ordering_count"]=> string(1) "3" ["close_hour"]=> array(3) { ["key"]=> string(4) "10pm" ["val"]=> string(8) "10:00 PM" ["value"]=> string(8) "10:00 PM" } ["open_hour"]=> array(3) { ["key"]=> string(3) "2pm" ["val"]=> string(8) "02:00 PM" ["value"]=> string(8) "02:00 PM" } ["day_open"]=> array(3) { ["key"]=> string(3) "Tue" ["val"]=> string(3) "Tue" ["value"]=> string(3) "Tue" } ["last"]=> string(1) "0" ["odd_even"]=> string(3) "odd" ["count"]=> int(3) }
[3]=> array(11) { ["id"]=> string(1) "4" ["created"]=> int(1418692529) ["updated"]=> bool(false) ["created_by"]=> string(1) "1" ["ordering_count"]=> string(1) "4" ["close_hour"]=> array(3) { ["key"]=> string(4) "10pm" ["val"]=> string(8) "10:00 PM" ["value"]=> string(8) "10:00 PM" } ["open_hour"]=> array(3) { ["key"]=> string(3) "2pm" ["val"]=> string(8) "02:00 PM" ["value"]=> string(8) "02:00 PM" } ["day_open"]=> array(3) { ["key"]=> string(3) "Wed" ["val"]=> string(3) "Wed" ["value"]=> string(3) "Wed" } ["last"]=> string(1) "0" ["odd_even"]=> string(4) "even" ["count"]=> int(4) }
[4]=> array(11) { ["id"]=> string(1) "5" ["created"]=> int(1418692536) ["updated"]=> bool(false) ["created_by"]=> string(1) "1" ["ordering_count"]=> string(1) "5" ["close_hour"]=> array(3) { ["key"]=> string(4) "10pm" ["val"]=> string(8) "10:00 PM" ["value"]=> string(8) "10:00 PM" } ["open_hour"]=> array(3) { ["key"]=> string(3) "2pm" ["val"]=> string(8) "02:00 PM" ["value"]=> string(8) "02:00 PM" } ["day_open"]=> array(3) { ["key"]=> string(3) "Thu" ["val"]=> string(3) "Thu" ["value"]=> string(3) "Thu" } ["last"]=> string(1) "0" ["odd_even"]=> string(3) "odd" ["count"]=> int(5) }
[5]=> array(11) { ["id"]=> string(1) "6" ["created"]=> int(1418692549) ["updated"]=> int(1418698491) ["created_by"]=> string(1) "1" ["ordering_count"]=> string(1) "6" ["close_hour"]=> array(3) { ["key"]=> string(4) "12am" ["val"]=> string(8) "12:00 AM" ["value"]=> string(8) "12:00 AM" } ["open_hour"]=> array(3) { ["key"]=> string(3) "2pm" ["val"]=> string(8) "02:00 PM" ["value"]=> string(8) "02:00 PM" } ["day_open"]=> array(3) { ["key"]=> string(3) "Fri" ["val"]=> string(3) "Fri" ["value"]=> string(3) "Fri" } ["last"]=> string(1) "0" ["odd_even"]=> string(4) "even" ["count"]=> int(6) }
[6]=> array(11) { ["id"]=> string(1) "7" ["created"]=> int(1418692559) ["updated"]=> int(1418698459) ["created_by"]=> string(1) "1" ["ordering_count"]=> string(1) "7" ["close_hour"]=> array(3) { ["key"]=> string(4) "12am" ["val"]=> string(8) "12:00 AM" ["value"]=> string(8) "12:00 AM" } ["open_hour"]=> array(3) { ["key"]=> string(3) "2pm" ["val"]=> string(8) "02:00 PM" ["value"]=> string(8) "02:00 PM" } ["day_open"]=> array(3) { ["key"]=> string(3) "Sat" ["val"]=> string(3) "Sat" ["value"]=> string(3) "Sat" } ["last"]=> string(1) "1" ["odd_even"]=> string(3) "odd" ["count"]=> int(7) }
}
["pagination"]=> NULL ["total"]=> int(7)
}
and in print_r
Array ( [entries] => Array (
[0] => Array ( [id] => 1 [created] => 1418692508 [updated] => [created_by] => 1 [ordering_count] => 1 [close_hour] => Array ( [key] => 10pm [val] => 10:00 PM [value] => 10:00 PM ) [open_hour] => Array ( [key] => 2pm [val] => 02:00 PM [value] => 02:00 PM ) [day_open] => Array ( [key] => Sun [val] => Sun [value] => Sun ) [last] => 0 [odd_even] => odd [count] => 1 )
[1] => Array ( [id] => 2 [created] => 1418692519 [updated] => 1418698391 [created_by] => 1 [ordering_count] => 2 [close_hour] => Array ( [key] => 10pm [val] => 10:00 PM [value] => 10:00 PM ) [open_hour] => Array ( [key] => 2pm [val] => 02:00 PM [value] => 02:00 PM ) [day_open] => Array ( [key] => Mon [val] => Mon [value] => Mon ) [last] => 0 [odd_even] => even [count] => 2 )
[2] => Array ( [id] => 3 [created] => 1418692525 [updated] => [created_by] => 1 [ordering_count] => 3 [close_hour] => Array ( [key] => 10pm [val] => 10:00 PM [value] => 10:00 PM ) [open_hour] => Array ( [key] => 2pm [val] => 02:00 PM [value] => 02:00 PM ) [day_open] => Array ( [key] => Tue [val] => Tue [value] => Tue ) [last] => 0 [odd_even] => odd [count] => 3 )
[3] => Array ( [id] => 4 [created] => 1418692529 [updated] => [created_by] => 1 [ordering_count] => 4 [close_hour] => Array ( [key] => 10pm [val] => 10:00 PM [value] => 10:00 PM ) [open_hour] => Array ( [key] => 2pm [val] => 02:00 PM [value] => 02:00 PM ) [day_open] => Array ( [key] => Wed [val] => Wed [value] => Wed ) [last] => 0 [odd_even] => even [count] => 4 )
[4] => Array ( [id] => 5 [created] => 1418692536 [updated] => [created_by] => 1 [ordering_count] => 5 [close_hour] => Array ( [key] => 10pm [val] => 10:00 PM [value] => 10:00 PM ) [open_hour] => Array ( [key] => 2pm [val] => 02:00 PM [value] => 02:00 PM ) [day_open] => Array ( [key] => Thu [val] => Thu [value] => Thu ) [last] => 0 [odd_even] => odd [count] => 5 )
[5] => Array ( [id] => 6 [created] => 1418692549 [updated] => 1418698491 [created_by] => 1 [ordering_count] => 6 [close_hour] => Array ( [key] => 12am [val] => 12:00 AM [value] => 12:00 AM ) [open_hour] => Array ( [key] => 2pm [val] => 02:00 PM [value] => 02:00 PM ) [day_open] => Array ( [key] => Fri [val] => Fri [value] => Fri ) [last] => 0 [odd_even] => even [count] => 6 )
[6] => Array ( [id] => 7 [created] => 1418692559 [updated] => 1418698459 [created_by] => 1 [ordering_count] => 7 [close_hour] => Array ( [key] => 12am [val] => 12:00 AM [value] => 12:00 AM ) [open_hour] => Array ( [key] => 2pm [val] => 02:00 PM [value] => 02:00 PM ) [day_open] => Array ( [key] => Sat [val] => Sat [value] => Sat ) [last] => 1 [odd_even] => odd [count] => 7 )
)
[pagination] => [total] => 7 )
taking only a few from that ghastly array:
$oldArray = array(
"entries"=> array(
array(
"id"=>"1",
"created"=> 1418692508,
"updated"=> false,
"created_by"=> "1",
"ordering_count"=> "1",
"close_hour" => array(
"key"=> "10pm",
"val"=> "10:00 PM",
"value"=> "10:00 PM"
),
"open_hour"=> array(
"key"=> "2pm",
"val"=> "02:00 PM",
"value"=> "02:00 PM"
),
"day_open"=> array(
"key"=> "Mon",
"val"=> "Mon",
"value"=> "Mon"
),
"last"=> "0",
"odd_even"=> "odd",
"count"=> 1
),
array(
"id"=>"1",
"created"=> 1418692508,
"updated"=> false,
"created_by"=> "1",
"ordering_count"=> "1",
"close_hour" => array(
"key"=> "10pm",
"val"=> "10:00 PM",
"value"=> "10:00 PM"
),
"open_hour"=> array(
"key"=> "2pm",
"val"=> "02:00 PM",
"value"=> "02:00 PM"
),
"day_open"=> array(
"key"=> "Tues",
"val"=> "Tues",
"value"=> "Tues"
),
"last"=> "0",
"odd_even"=> "odd",
"count"=> 1
),
array(
"id"=>"1",
"created"=> 1418692508,
"updated"=> false,
"created_by"=> "1",
"ordering_count"=> "1",
"close_hour" => array(
"key"=> "10pm",
"val"=> "10:00 PM",
"value"=> "10:00 PM"
),
"open_hour"=> array(
"key"=> "2pm",
"val"=> "02:00 PM",
"value"=> "02:00 PM"
),
"day_open"=> array(
"key"=> "Weds",
"val"=> "Weds",
"value"=> "Weds"
),
"last"=> "0",
"odd_even"=> "odd",
"count"=> 1
),
),
"pagination"=> NULL,
"total"=> 7
);
$hours = array();
foreach ($oldArray['entries'] as $e) {
$h = '';
if (isset($e['open_hour']['value'])) {
$h = $e['open_hour']['value'].'-'.$e['close_hour']['value'];
}
$hours[$e['day_open']['value']] = array(
$h,
);
}
echo "<pre>";print_r($hours);
prints the following:
Array
(
[Mon] => Array
(
[0] => 02:00 PM-10:00 PM
)
[Tues] => Array
(
[0] => 02:00 PM-10:00 PM
)
[Weds] => Array
(
[0] => 02:00 PM-10:00 PM
)
)
Something like this? If I understood your dump correctly...
$days = [];
foreach ($this-entries["entries"] as $entry) {
$days[strtolower($entry['day_open']['value'])] = [
sprintf(
"%s-%s",
substr($entry['open_hour']['value'], 0, 5),
substr($entry['close_hour']['value'], 0, 5)
)
];
}
var_dump($days);
Like I thought the
["pagination"]=> NULL ["total"]=> int(7)
was the problem giving you an extra row, simply only go through the array if you have days
if(count($entry) == 7)
Here is your code modified:
foreach ($entries as $entry) {
if(count($entry) == 7)
{
for ($i = 0; $i < count($entry); $i++) {
$day = $entry[$i]['day_open']['value'];
$open = $entry[$i]['open_hour']['value'];
$close = $entry[$i]['close_hour']['value'];
print $day . " : " . $open . " to " . $close . "<br>";
}
}
How could I convert this array:
$a = array(
0 => array(
0 => 31,
1 => 39
),
1 => array(
0 => 41
)
);
to this one:
$a = array(
0 => array(
0 => array(
0 => 31,
1 => 41
),
1 => array(
0 => 39,
1 => 41
)
),
1 => array(
0 => array(
0 => 41,
1 => 31
),
1 => array(
0 => 41,
1 => 39
)
)
);
I tried several ways, but find no proper solution. At the moment my brain is overheated. So maybe someone has a solution for me.
Thanks #Manos.
Unfortunatly there are dynamic arrays. So these static function wont work for me.
So the array could also look like this:
$a = array(
0 => array(
0 => 31,
1 => 39
),
1 => array(
0 => 41,
1 => 49,
2 => 51
),
2 => array(
0 => 73
)
);
Output should look like this:
$a = array(
0 => array(
0 => array(
0 => 31,
1 => 41,
2 => 73
),
1 => array(
0 => 31,
1 => 49,
2 => 73
),
2 => array(
0 => 31,
1 => 51,
2 => 73
),
3 => array(
0 => 39,
1 => 41,
2 => 73
),
4 => array(
0 => 39,
1 => 49,
2 => 73
),
5 => array(
0 => 39,
1 => 51,
2 => 73
),
1 => array(
0 => array(
0 => 41,
1 => 31,
2 => 73
),
1 => array(
0 => 41,
1 => 39,
2 => 73
),
2 => array(
0 => 49,
1 => 31,
2 => 73
),
3 => array(
0 => 49,
1 => 39,
2 => 73
),
etc ......
)
);
Manos function Output:
array(3) {
[0]=>
array(8) {
[0]=>
array(2) {
[0]=>
int(31)
[1]=>
int(41)
}
[1]=>
array(2) {
[0]=>
int(39)
[1]=>
int(41)
}
[2]=>
array(2) {
[0]=>
int(31)
[1]=>
int(49)
}
[3]=>
array(2) {
[0]=>
int(39)
[1]=>
int(49)
}
[4]=>
array(2) {
[0]=>
int(31)
[1]=>
int(51)
}
[5]=>
array(2) {
[0]=>
int(39)
[1]=>
int(51)
}
[6]=>
array(2) {
[0]=>
int(31)
[1]=>
int(73)
}
[7]=>
array(2) {
[0]=>
int(39)
[1]=>
int(73)
}
}
[1]=>
array(9) {
[0]=>
array(2) {
[0]=>
int(41)
[1]=>
int(31)
}
[1]=>
array(2) {
[0]=>
int(49)
[1]=>
int(31)
}
[2]=>
array(2) {
[0]=>
int(51)
[1]=>
int(31)
}
[3]=>
array(2) {
[0]=>
int(41)
[1]=>
int(39)
}
[4]=>
array(2) {
[0]=>
int(49)
[1]=>
int(39)
}
[5]=>
array(2) {
[0]=>
int(51)
[1]=>
int(39)
}
[6]=>
array(2) {
[0]=>
int(41)
[1]=>
int(73)
}
[7]=>
array(2) {
[0]=>
int(49)
[1]=>
int(73)
}
[8]=>
array(2) {
[0]=>
int(51)
[1]=>
int(73)
}
}
[2]=>
array(5) {
[0]=>
array(2) {
[0]=>
int(73)
[1]=>
int(31)
}
[1]=>
array(2) {
[0]=>
int(73)
[1]=>
int(39)
}
[2]=>
array(2) {
[0]=>
int(73)
[1]=>
int(41)
}
[3]=>
array(2) {
[0]=>
int(73)
[1]=>
int(49)
}
[4]=>
array(2) {
[0]=>
int(73)
[1]=>
int(51)
}
}
}
foreach ($a as $first_group_key => $first_group) {
foreach ($a as $second_group_key => $second_group) {
if ($second_group_key == $first_group_key) {
continue;
}
$i = count($b[$first_group_key]);
foreach ($second_group as $second_value) {
foreach ($first_group as $first_key => $first_value) {
$b[$first_group_key][$i][0] = $first_value;
$b[$first_group_key][$i][1] = $second_value;
$i++;
}
}
}
}