Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I've looked on this site and have found a few examples of merging arrays but to be honest, I'm not sure which one is right for me.
I am having a difficult time trying to merge these arrays. I've tried array_combine and array_merge but I am not having any luck.
What I need is a single array with the output like this:
Array
(
[0] => Array
(
[UnitNo] => 91
[Name] => Receiving
[ActivityNo] =>
[Active] => 2
[CallNo] =>
[CallStatusNo] =>
[Assigned] =>
[UnitId] => 2
)
[1] => Array
(
[UnitNo] => 83
[Name] => Shipping
[ActivityNo] =>
[Active] =>
[CallNo] =>
[CallStatusNo] =>
[Assigned] =>
[UnitId] => 1
)
)
These are the two arrays that I need to merge together based on the UnitId that should form a complete array like the one above. I just don't know how to do this. If someone could direct me a bit, I would be grateful.
// Array #1
Array
(
[0] => Array
(
[UnitId] => 2
[UnitNo] => 91
[Name] => Receiving
[Active] => 4
)
[1] => Array
(
[UnitId] => 1
[UnitNo] => 83
[Name] => Shipping
[Active] => 4
)
)
// Array #2
Array
(
[0] => Array
(
[UnitId] => 2
[ActivityNo] => 1
[CallNo] => 1
[CallStatusNo] => 1
[Assigned] => 1
)
[1] => Array
(
[UnitId] => 1
[ActivityNo] => 11
[CallNo] => 2
[CallStatusNo] => 1
[Assigned] => 1
)
)
This isn't a case where you can just use a php function and call it good - you will have to do some custom logic of your own.
This would work, but only if both $array1 and $array2 have the same items at each index.
$array1;
$array2;
$mergedArray;
for ($i=0; $i++; $i < count($array1) - 1) {
//first add all the values from array1
$mergedArray[$i] = $array1[$i];
foreach($array2[$i] as $array2Key=>$array2Value){
//now check for missing key/values that are in array2 but not in array1
if (array_key_exists($array2Key, $array1[$i]) == false) {
// the key does not exist in array1, so add it to mergedArray
$mergedArray[$i][$array2Key] = $array2Value;
}
}
}
It would be safer if your arrays were associative arrays, indexed by a unique value.
Related
This question already has answers here:
PHP Associative Array Duplicate Keys
(6 answers)
Closed last month.
I have an array with multiple key.
Array 1
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
Output
Array ( [Peter] => 35 [Ben] => 37 [Joe] => 43 )
Expected Output
$age = array("value"=>"35", "value"=>"37", "value"=>"43");
Array ( [value] => 35 [value] => 37 [value] => 43 )
You can't
Indeed, array keys must be unique. Otherwise, what should the program output when you try to access value ?
But ...
If you need to store a list of value for one key, you can use array of arrays.
$array = array("value" => array());
array_push($array["value"], 35, 40, 53);
print_r($array)
And the output will be:
Array
(
[value] => Array
(
[0] => 35
[1] => 40
[2] => 53
)
)
Only way is to turn this array into 2D array:
$age = array(
array("value" => "35"),
array("value" => "37"),
array("value" => "43")
);
-- Output --
Array
(
[0] => Array
(
[value] => 35
)
[1] => Array
(
[value] => 37
)
[2] => Array
(
[value] => 43
)
)
-- Usage --
$age[0]['value'];
$age[1]['value'];
$age[2]['value'];
But it completely depends if $age array is in our control and can be changed.
You can't
Array keys must be unique.
http://php.net/manual/en/language.types.array.php
If multiple elements in the array declaration use the same key, only the last one will be used as all others are overwritten.
Yes, array key should be unique. So, whatever you are asking it's not possible. Can you tell us what's requirements? So that, people can suggest any alternate solution.
This cannot be possible with native php arrays. What you need is a multimap, and you can find several implementations of it on github.
For example: link
EDIT: the link above is an interface. you need to include also link2
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have the following array that i want to re-arrange
Array
(
[0] => stdClass Object
(
[feeds_id] => 1338
[flag] => 0
)
[1] => stdClass Object
(
[feeds_id] => 1339
[flag] => 0
)
[2] => stdClass Object
(
[feeds_id] => 1339
[flag] => 1
)
)
I want to arrange it to look like this
[1338] => Array (
[0] => 0
)
[1339] => Array (
[0] => 0
[1] => 1
)
This code should work:
$newArray=array();
foreach($items as $item){
if(!is_array($newArray[$item->feeds_id])){
$newArray[$item->feeds_id]=array();
}
array_push($newArray[$item->feeds_id],$item->flag);
}
You should first create an empty array where the new data will be stored. Then, inside the foreach, you should use array_push, BUT if the sub-array in where you want to put the data is not an array, you should declare it first (that's why the "if" before the array_push)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array that returns keys and values.
<?php
print_r(array_values($arResult["DISPLAY_PROPERTIES"]));
$arresults = $arResult["DISPLAY_PROPERTIES"];
?>
The code above would show this result:
Array ([0] => Array
([ID] => 29
[TIMESTAMP_X] => 2014-08-26 08:23:13
[IBLOCK_ID] => 3
[NAME] => ProductID
[ACTIVE] => Y
[SORT ] => 500
[CODE] => product_id
[DEFAULT_VALUE] =>
[PROPERTY_TYPE] => S
[ROW_COUNT] => 1
[COL_COUNT] => 30
[LIST_TYPE] => L
[MULTIPLE] => N
[XML_ID] => [file_type] =>
[MULTIPLE_CNT] => 5
[TMP_ID] =>
[LINK_IBLOCK_ID] => 0
[WITH_DESCRIPTION] => N
[SEARCHABLE] => N
[filtrable] => N
[IS_REQUIRED] => N
[VERSION] = > 1
[user_type] =>
[USER_TYPE_SETTINGS] =>
[HINT] =>
[PROPERTY_VALUE_ID] => 11880
[VALUE] => PCA.CD08.21.A1
[DESCRIPTION] =>
[VALUE_ENUM] =>
[VALUE_XML_ID] =>
[VALUE_SORT] =>
[~ VALUE] => PCA.CD08.21.A1
[~ DESCRIPTION] =>
[~ NAME] => ProductID
[~ DEFAULT_VALUE] =>
[display_value] => PCA.CD08.21.A1)
)
Problem: How can I show the value PCA.CD08.21.A1 .. What php code do i use to display only PCA.CD08.21.A1 ?
Kindly Please Help Me...
You can simply access the value directly:
echo $arResult['DISPLAY_PROPERTIES'][0]['VALUE'];
However, you have to replace index 0 with the proper index, since calling array_values() on $arResult will numerically reindex it.
foreach($arResult["DISPLAY_PROPERTIES"] as $key => $value)
{
if($key == 'VALUE')
echo $value;
}
Is this what you need?
$display_value = $arResult["DISPLAY_PROPERTIES"][0]["display_value"]
Keeping in mind that we do not know what the first key in the array is you can do the following:
$arr = array_shift($arResult["DISPLAY_PROPERTIES"]);
$display_value = $arr["display_value"];
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've got a problem.
I have got this array:
Array (
[0] => Array (
[id] => 1
[opt] => reg_limit
[value] => 0
)
[1] => Array (
[id] => 3
[opt] => pages_offline
[value] => []
)
[2] => Array (
[id] => 4
[opt] => devolp
[value] => TRUE
)
)
I want to check if the [opt] devolp has the [value] TRUE in the third array. How can I do?
$aArray = Array (
[0] => Array (
[id] => 1
[opt] => reg_limit
[value] => 0
)
[1] => Array (
[id] => 3
[opt] => pages_offline
[value] => []
)
[2] => Array (
[id] => 4
[opt] => devolp
[value] => TRUE
)
)
foreach($aArray AS $aInnerArray){
if($aInnerArray['opt'] == 'devolp' && $aInnerArray['value'] == TRUE){
//YOUR USE CASE
}
}
if ($array[2]['value']) echo 'true';
Since the OP's question is quite vague on the details wether or not he knows which array key he needs to check,
Here is a simple example that you can use if you know the array key that you need to check in.
$bool = $yourMultiDeminsionalArray[2]['value'];
if ($bool) {
//Do some awesome PHP shizzle here
}
Provided you will work with a big array in the future and need some flexibility, this foreach will work for you:
foreach($array as $a) {
if(array_key_exists("opt", $a) && $a['opt'] == "devolp") {
if(array_key_exists("value", $a) && $a['value'] == TRUE) {
echo "Found it!";
//Do whatever you need to do here....
}
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am seeing some weird behavior:
I have 2 sessions that I set with some value: $_SESSION['shippingZip'] and $_SESSION['shippingOption'].
Then my code goes through this code to post the input values sent in a form:
$shippingOption = $_POST['shippingOption'];
Print_r ($_SESSION);
$shippingZip = $_POST['shippingZip'];
Those POSTs are coming empty in this pass. However, the Print shows my session $_SESSION['shippingOption'] empty, when it should show the string that had previously been assigned to it.
-------------------------- POSTING FULL PROOF
Sessions are loaded with some data:
$_SESSION['shippingOption'] = $shippingOption;
$_SESSION['shippingZip']= $shippingZip;
Then:
Print_r ($_SESSION);
$shippingOption = $_POST['shippingOption'];
Print_r ($_SESSION);
$shippingZip = $_POST['shippingZip'];
Print_r ($_SESSION);
Output:
Array ( [itemAdded] => 1 [Payment_Amount] => 46.52 [cart] => Array ( [4] => Array ( [itemId] => 4 [qty] => 1 ) ) [shippingOption] => FIRST CLASS [shippingZip] => 10025 [shippingPrice] => 1.52 )
Array ( [itemAdded] => 1 [Payment_Amount] => 46.52 [cart] => Array ( [4] => Array ( [itemId] => 4 [qty] => 1 ) ) [shippingOption] => [shippingZip] => 10025 [shippingPrice] => 1.52 )
Array ( [itemAdded] => 1 [Payment_Amount] => 46.52 [cart] => Array ( [4] => Array ( [itemId] => 4 [qty] => 1 ) ) [shippingOption] => [shippingZip] => [shippingPrice] => 1.52
You can clearly see how after each POST, the SESSION with the same name loses its value. It's totally crazy!!!
$_POST does not directly populate $_SESSION. You need to assign the values to the session
i.e.
$_SESSION['shippingOption'] = $_POST['shippingOption'];
EDIT
After you posted more code, it looks like you are not defining $shippingOption; before setting it $_SESSION['shippingOption'] = $shippingOption;
Make sure the order goes like this:
$shippingOption = $_POST['shippingOption'];
$_SESSION['shippingOption'] = $shippingOption;