Data is coming from 2 differents SQL request, so i have 2 arrays like :
FIRST ARRAY
array(6) {
[0]=>
array(2) {
["edible"]=>
string(3) "600"
["Food"]=>
string(4) "Fruit"
}
[1]=>
array(2) {
["edible"]=>
string(3) "500"
["Food"]=>
string(6) "Vegetables"
}
[2]=>
array(2) {
["edible"]=>
string(4) "1000"
["Food"]=>
string(3) "meat"
}
...
SECOND ARRAY
array(5) {
[0]=>
array(2) {
["out-of-date"]=>
string(3) "17"
["Food"]=>
string(4) "Fruit"
}
[1]=>
array(2) {
["out-of-date"]=>
string(3) "54"
["Food"]=>
string(3) "Vegetables"
}
[2]=>
array(2) {
["out-of-date"]=>
string(2) "60"
["Food"]=>
string(3) "meat"
}
...
What i would like is to merge the two arrays, but not with a function like array_merge or array_merge_recursive.
Because i would like to just add a row (key + value) like
["out-of-date"]=>
string(3) "17""
in the first array if we have the same Food (key)
output result desired :
array(6) {
[0]=>
array(3) {
["out-of-date"]=>
string(3) "17"
["edible"]=>
string(3) "600"
["Food"]=>
string(4) "Fruit"
}
...
You need to make a loop inside loop and verify the food key, if is equal merge the values to another array, Try this code:
<?php
$array1 = array();
$array1[] = array('food' => 'Fruit', 'edible' => '600');
$array1[] = array('food' => 'Vegetables', 'edible' => '500');
$array1[] = array('food' => 'meat', 'edible' => '700');
$array2 = array();
$array2[] = array('food' => 'Fruit', 'out-of-date' => '17');
$array2[] = array('food' => 'Vegetables', 'out-of-date' => '15');
$array_merged = array();
foreach ($array1 as $key => $value) {
$array_merged[$key] = $value;
foreach ($array2 as $ke => $val) {
if ($val['food'] == $value['food']) {
$array_merged[$key]['out-of-date'] = $val['out-of-date'];
}
}
}
print_r($array_merged);
I'm not sure about structure of your database table. But let's you have two tables in your MySql database:
First - edible | Food
Second - out_of_date | Food
So you can query your array by using join mysql operation. You need sql like below:
SELECT *
FROM First
LEFT JOIN Second ON First.Food=Second.Food
Also, you can use additional conditions for all tables like:
SELECT *
FROM First
LEFT JOIN Second ON First.Food=Second.Food
WHERE First.edible = 1000 AND Second.out_of_date = 10
You can find more information here: http://dev.mysql.com/doc/refman/5.7/en/join.html
Related
I have an array $arrItems['items'] in which 5 more arrays (associate array) and each array contain 5 element (with the keys: f_name, l_name, contact, address, seller_id).
I want to get all those arrays (from $arrItems['items']) in which element of seller_id is 1 like "seller_id"=>"1,2,3" OR "seller_id"=>"3,2,1" OR "seller_id"=>"4,6,2" OR "seller_id"=>"5,3,4" OR "seller_id"=>"2,1,2" Array given below.
array(5)
{
[0] =>
array(5)
{
["f_name"] =>
string(3) "abc"
["l_name"] =>
string(3) "xyz"
["contact"] =>
string(5) "12345"
["address"] =>
string(3) "xyz"
["seller_id"] =>
string(1) => "1,2,3"
}
[1]=>
array(5) {
["f_name"]=>
string(3) "abc"
["l_name"]=>
string(3) "xyz"
["contact"]=>
string(5) "12345"
["address"]=>
string(3) "xyz"
["seller_id"]=>
string(1)=>"3,2,1"
}
[2]=>
array(5) {
["f_name"]=>
string(3) "abc"
["l_name"]=>
string(3) "xyz"
["contact"]=>
string(5) "12345"
["address"]=>
string(3) "xyz"
["seller_id"]=>
string(1)=>"4,6,2"
}
[3]=>
array(5) {
["f_name"]=>
string(3) "abc"
["l_name"]=>
string(3) "xyz"
["contact"]=>
string(5) "12345"
["address"]=>
string(3) "xyz"
["seller_id"]=>
string(1)=>"5,3,4"
}
[4]=>
array(5) {
["f_name"]=>
string(3) "abc"
["l_name"]=>
string(3) "xyz"
["contact"]=>
string(5) "12345"
["address"]=>
string(3) "xyz"
["seller_id"]=>
string(1)=>"2,1,2"
}
Kindly Help me it actually order table i want to just pick those array in which current seller id is 1 . for example seller No # 1 is login. then all those array select mean those array save in other array.
array_filter (documentation) with in_array (documentation):
$sellerId = "1";
$arr = array_filter($arrItems['items'], function($e) use ($sellerId) {
return in_array($sellerId, explode(",", $e["seller_id"]);
});
If you want only those 5 option use:
$options = array("1,2,3", "3,2,1", "5,3,4", "4,6,2", "2,1,2");
$arr = array_filter($arrItems['items'], function($e) use ($options ) {
return in_array($e["seller_id"], $options);
});
Edited: As for your request this is version of the same code using foreach loop:
$sellerId = "1";
$seller_order_arr = [];
foreach ($arrItems['items'] as $row) {
if (in_array($sellerId, explode(",", $row["seller_id"])))
$seller_order_arr[] = $row;
}
Now $seller_order_arr will hold your filtered array
If I see that correctly, your seller_id value is a concatenated string of Ids. You can filter your outer array with array_filter and a custom callback function, which should return true, if the element should be kept.
$idToLookup = 1;
$filteredItems = array_filter($arrItems['items'], function($element) {
return preg_match('/(^|,)' . $idToLookup . '($|,)/', $element['seller_id']);
]);
Or if you are not so familiar with regex, first explode the number-string into individual numbers and then use in_array:
$idToLookup = 1;
$filteredItems = array_filter($arrItems['items'], function($element) {
$sellerIds = explode(',', $element['seller_id']);
return in_array($idToLookup, $sellerIds);
]);
Use array_filter()
$array = array_filter($arrItems['items'], function ($item) {
$ids = explode(',', $item['seller_id']);
return in_array(1, $ids);
});
I have a big array which looks like this:
array(2) {
["Final Fantasy VII"]=>
array(5) {
["rows"]=>
array(2) {
[0]=>
array(6) {
["price"]=>
string(5) "11.69"
["price_old"]=>
string(4) "4.66"
["currency"]=>
string(4) "euro"
["portal"]=>
string(0) ""
["link"]=>
string(77) "https://de.gamesplanet.com/game/final-fantasy-vii-download--1001-1?ref=gmkeys"
["shop"]=>
string(4) "9507"
}
[1]=>
array(6) {
["price"]=>
string(5) "14.99"
["price_old"]=>
...
}
}
}
["Battlefield 1"]=>
array(3) {
["rows"]=>
array(2) {
[0]=>
array(6) {
["price"]=>
...
}
[1]=>
array(6) {
["price"]=>
...
}
}
}
}
And I want to get only certain parts of this array where the name is matching my searched title. So, I use this code for that:
function createACFRepeater($title){
$repeater = array();
if(searchForGameXML($title)){
$count = count($GLOBALS["productsXML"][$title]['rows']);
for($i = 0; $i < $count; $i++){
array_push($repeater, $GLOBALS["productsXML"][$title]['rows'][$i]);
}
return $repeater;
}else{
return $repeater;
}
}
My problem now is that the the $repeater array looks like this:
array(2) {
[0]=>
array(6) {
["price"]=>
string(5) "19.98"
["price_old"]=>
...
}
[1]=>
array(6) {
["price"]=>
string(4) "7.99"
["price_old"]=>
...
}
}
There is a numeric key which is pointing to the array [0] => .... But what I want is simply an array in a array without any associative relations...
How can I create an array which looks like this:?
array(2) {
array(6) {
["price"]=>
string(5) "19.98"
["price_old"]=>
...
}
array(6) {
["price"]=>
string(4) "7.99"
["price_old"]=>
...
}
}
Greetings and Thank You!
According to the array definition it is impossible. Any array item must have key and value, documentation for array starts from:
An array in PHP is actually an ordered map. A map is a type that associates values to keys
You will always have numeric keys. As #lubart already said: it's impossible to have an array without keys. Btw., all of the the follwing arrays are completely equal:
$array1 = array([0] => array([0] => 'hi', [1] => array([0] => '23.5')));
$array2 = array(array('hi', array('23.5')));
$array3 = [['hi', ['23.5']]];
$array4 = [ [0] => [ [0] => 'hi', [1] => [ [0] => '23.5' ] ] ];
I have the following array:
array(5) {
[0]=> array(3) {
[0]=> string(10) "2013-09-18"
[1]=> string(75) "Ready For Retina HD: Create Pixel-Perfect Assets For Multiple Scale Factors"
[2]=> string(74) "ready-for-retina-hd-create-pixel-perfect-assets-for-multiple-scale-factors"
}
[1]=> array(3) {
[0]=> string(10) "2010-10-20"
[1]=> string(40) "Taking A Closer Look At Tech Conferences"
[2]=> string(40) "taking-a-closer-look-at-tech-conferences"
}
[2]=> array(3) {
[0]=> string(10) "2014-10-19"
[1]=> string(29) "Wayfinding For The Mobile Web"
[2]=> string(29) "wayfinding-for-the-mobile-web"
}
[3]=> array(3) {
[0]=> string(10) "2014-05-15"
[1]=> string(24) "Freebie: Icons Of Autumn"
[2]=> string(23) "freebie-icons-of-autumn"
}
[4]=> &array(1){
[0]=> string(0) ""
}
}
How would I go about assigning key names to each part of the inner array? E,g date, title, pathname.
I understand you can do something like this to create an array with certain keys, but how does this work with multiple nested arrays? And how can it be assigned after array creation?
$keys = array('Date', 'Title', 'Filepath');
Assuming $array1 is your main array (with 5 values).
foreach($array1 as $a)
{
if (len($a) == 3)
$array2[] = array("Date" => $a[0], "Title" => $a[1], "Filepath" => $a[2]);
}
$array1 = $array2;
Use a foreach.
$new = [];
foreach ($origArray as $inner) {
$new[] = [
"date" => $inner[0],
"title" => $inner[1],
"filepath" => $inner[2]
];
}
$origArray = $new;
This doesn't handle cases where the item does not conform to the "standard" (e.g item 4) but this should get you started.
Say I have an array that looks like this:
array(100) {
[0]=>
array(4) {
["player_id"]=>
string(2) "jk"
["date"]=>
string(10) "2012-11-07"
["hits"]=>
string(4) "1000"
}
[1]=>
array(4) {
["player_id"]=>
string(2) "jk"
["date"]=>
string(10) "2012-11-14"
["hits"]=>
string(4) "2000"
}
[2]=>
array(4) {
["player_id"]=>
string(2) "mc"
["date"]=>
string(10) "2012-11-14"
["hits"]=>
string(4) "1500"
}
[3]=>
array(4) {
["player_id"]=>
string(2) "mc"
["date"]=>
string(10) "2012-11-07"
["hits"]=>
string(4) "2300"
}
...
And this continues. So, basically I have several players and for each player I have two dates. I would like to end up having an array like this:
array(100) {
["jk"]=>
array(2) {
["hits_today"]=>
string(4) "1000"
["hits_difference"]=>
string(5) "-1000"
}
["mc"]=>
array(2) {
["hits_today"]=>
string(4) "1500"
["hits_difference"]=>
string(4) "-800"
}
..
Basically I want to process the first array knowing that for each player I (might) have two values, and create an array of arrays that as keys uses the player_id and as components with the difference between those two values, and the value of today.
My concerns are:
What is the best way to process the first array?
How would I manage if the value of today doesn't exist, or the value of tomorrow doesn't? How can I control that while processing the array?
If this comes from a query I would do this instead:
SELECT player_id,
SUM(IF(`date`='$yesterday', hits, 0)) AS hits_yesterday,
SUM(IF(`date`='$today', hits, 0)) AS hits_today
FROM ...
GROUP BY player_id
I needed to preserve the date in the output array to determine if the current date being calculated was > or < the date already on hand, but this is the solution I came up with:
$array = array(
array("player_id" => "jk", "date" => "2012-11-07", "hits" => "1000"),
array("player_id" => "jk", "date" => "2012-11-14", "hits" => "2000"),
array("player_id" => "mc", "date" => "2012-11-14", "hits" => "1500"),
array("player_id" => "mc", "date" => "2012-11-07", "hits" => "2300")
);
$output = array();
foreach($array as $k=>$v){
if(!isset($output[$v['player_id']])){
$output[$v['player_id']]['hits_today'] = $v['hits'];
$output[$v['player_id']]['hits_difference'] = 0;
$output[$v['player_id']]['date'] = $v['date'];
}elseif($v['date'] < $output[$v['player_id']]['date']){
$output[$v['player_id']]['hits_difference'] = $output[$v['player_id']]['hits_today'] - $v['hits'];
}elseif($v['date'] > $output[$v['player_id']]['date']){
$output[$v['player_id']]['hits_difference'] = $output[$v['player_id']]['hits_today'] - $v['hits'];
$output[$v['player_id']]['hits_today'] = $v['hits'];
$output[$v['player_id']]['date'] = $v['date'];
}
}
echo '<pre>';
print_r($output);
echo '</pre>';
?>
I got an $actions array, and $actions_used array.
$actions_used looks like this:
array(1) {
[2]=>
string(1) "18"
[5]=>
string(1) "33"
}
$actions looks like this:
array(3) {
[1]=>
string(9) "Withdraw"
[2]=>
string(13) "Deposit"
[5]=>
string(10) "Blabla"
}
I would like to sort $actions based on the value that is in $actions_used.
The correct output would be for $actions:
array(3) {
[5]=>
string(9) "Blabla"
[2]=>
string(13) "Deposit"
[1]=>
string(10) "Withdraw"
}
Why? Because array key 5, "Blabla" has the biggest value "33" and then comes array key "2" which has value 18 and then at last comes array key 1, "Withdraw" which have 0 (no value)
How can this be done?
This should do the trick.
$sorted_actions = array();
asort($actions_used);
foreach($actions_used AS $key => $amount) {
$sorted_actions[] = array('amount' => $amount, 'action' => $actions[$key]);
unset($actions[$key]);
}
$sorted_actions = $sorted_actions + $actions;
How would something like this work?
arsort($action_used);
foreach ($action_used as $k => $v) {
$newArray[$k] = $actions[$k];
unset($action_used[$k];
}
$newArray = array_merge($newArray, $action_used);
I think you could sort the second array with uksort and actually compare the values from the first array in the custom comparer
e.g.:
uksort($arr2, function($i1, $i2) {
return $arr1[$i1] - $arr1[$i2];
});