I have an array as follows:
array(1) {
[0]=> string(1) "2"
[1]=> string(1) "1"
[2]=> string(1) "3"
[3]=> string(1) "4"
[4]=> string(1) "5"
[5]=> string(1) "6"
[6]=> string(1) "7"
}
}
What I would like to do is order a second Array based on the above (which alters with user selection):
Array {
"Two" => "Two",
"One" => "One",
"Three" => "Three",
"Four" => "Four",
"Five" => "Five",
"Six" => "Six"
"Seven" => "Seven"
}
EDIT: The user access the page where there is a jQuery Sortable list - they order it as they please and this is saved to the DB, what I'm trying to do now is when the user comes back to the page is make sure the list is in the order they set out. So it is the order they set.
The second array is the array that populate the list on the .PHP page and that's why I need it to match the first array.
I've looked at array_multisort but no joy.
I'm assuming I'd do some of Loop in PHP to populate the second array before using it but I'm struggling - any suggestions?
I guess array_combine is what you are searching for:
$indexes = array('2','1');
$indexes = array_merge($indexes, range(3,7)); // used range to fill missing indexes
$values = array('two', 'one', 'three', 'four', 'five', 'six', 'seven');
$result=array_combine($indexes, $values);
ksort($result); // sort by keys
print_r($result);
// Array
// (
// [1] => one
// [2] => two
// [3] => three
// [4] => four
// [5] => five
// [6] => six
// [7] => seven
// )
I do not fully understand your goal but I think the following code will produce your desired result
$data = array("two", "one", "three", "seven");
$result = array();
foreach ( $data as $item) {
$result[$item] = $item;
}
print_r($result);
This will output
Array
(
[two] => two
[one] => one
[three] => three
[seven] => seven
)
Related
I have array in this structure, and I want delete item[6] from array.
I used unset($arrayname[6]) in php but it's not working. Any help? Is there any way I can remove element [6] from array?
array(2) {
[6]=>
array(2) {
["id"]=>
string(1) "1"
["day"]=>
string(6) "Monday"
}
[7]=>
array(2) {
["id"]=>
string(1) "2"
["day"]=>
string(7) "Tuesday"
}}
This works as you would expect:
<?php
$days =
array (
6 =>
array (
'id' => 1,
'day' => 'Mon',
),
7 =>
array (
'id' => 2,
'day' => 'Tue',
)
);
unset($days[6]);
var_export($days);
Output:
array (
7 =>
array (
'id' => 2,
'day' => 'Tue',
),
)
You can use unset() function which removes the element from an array and then use array_values() function which indexes the array numerically.
$array2 = {{array of array}}
// remove item at index 6 which is 'for'
unset($array2[6]);
// Print modified array
var_dump($array2);
// Re-index the array elements
$arr2 = array_values($array2);
//Print re-indexed array
var_dump($array2);
This solution will work, please check and let me know.
I have two arrays:
One:
array(4) {
[0]=> array(2) {
[0]=> string(19) "Ford"
[1]=> string(1) "1"
}
[1]=> array(2) {
[0]=> string(15) "Chevrolet"
[1]=> string(1) "1"
}
[2]=> array(2) {
[0]=> string(7) "VW"
[1]=> string(1) "1"
}
[3]=> array(2) {
[0]=> string(4) "Fiat"
[1]=> string(1) "3"
}
}
Two:
array(6) {
[0]=> string(7) "#581845"
[1]=> string(7) "#900C3F"
[2]=> string(7) "#C70039"
[3]=> string(7) "#FF5733"
[4]=> string(7) "#FFC300"
[5]=> string(7) "#DAF7A6"
}
Now, I need the first array combining with the second, excluding the elements of the second array that are not used by the first. At the end, I want to receive an array like:
[0]=> {
[0]=> "Ford",
[1]=> "1",
[2]=>"#581845"
}
[1]=>
...
}
Provided you always have more colors than auto makes, you can do this:
$makes = [
[
"Ford",
"1"
],
[
"Chevrolet",
"1"
],
[
"VW",
"1"
],
[
"Fiat",
"3"
]
];
$colors = [
"#581845",
"#900C3F",
"#C70039",
"#FF5733",
"#FFC300",
"#DAF7A6"
];
foreach($makes as &$currMakeTuple)
{
$currMakeTuple[] = array_shift($colors);
}
print_r($makes);
Array
(
[0] => Array
(
[0] => Ford
[1] => 1
[2] => #581845
)
[1] => Array
(
[0] => Chevrolet
[1] => 1
[2] => #900C3F
)
[2] => Array
(
[0] => VW
[1] => 1
[2] => #C70039
)
[3] => Array
(
[0] => Fiat
[1] => 3
[2] => #FF5733
)
)
You should probably check that condition and have a contingency for it.
Another one solution. Independent of the number of colors. Will add to brands only those colors which keys are equal to the brands' keys.
$brands = [
['Ford', 1],
['Chevrolet', 1],
['VW', 1],
['Fiat', 3],
];
$colors = [
'#581845',
'#900C3F',
'#C70039',
'#FF5733',
'#FFC300',
'#DAF7A6',
];
array_walk($brands, function(&$value, $key, $colors) {
if (isset($colors[$key])) {
$value = array_merge($value, [$colors[$key]]);
}
}, $colors);
print_r($brands);
I'm not saying you should do it this way, but I thought it was worth showing an alternative that used array functions instead:
var_dump(array_map(function($make, $color) {
$make[] = $color;
return $make;
}, $makes, array_slice($colors, 0, count($makes))));
I used:
array_slice to reduce the $colors array, should it be too long.
array_map with a lambda (anonymous function) that will apply the lambda to each element and return the final array upon completion.
I have an array
[0] => array(3) {
["id"] => string(1) "2"
["name"] => string(10) "Contractor"
["statuses"] => array(4) {
[1] => array(3) {
["id"] => string(1) "1"
["name"] => string(3) "NEW"
["count"] => string(2) "32"
}
[3] => array(3) {
["id"] => string(1) "3"
["name"] => string(8) "RETURNED"
["count"] => string(2) "20"
}
[5] => array(3) {
["id"] => string(1) "5"
["name"] => string(6) "FAILED"
["count"] => string(2) "46"
}
[58] => array(3) {
["id"] => string(2) "58"
["name"] => string(6) "REVISE"
["count"] => string(3) "197"
}
}
}
now when I convert into JSON it look like this
"items":[{"id":"2","name":"Contractor","statuses":{"1":{"id":"1","name":"NEW","count":"32"},"3":{"id":"3","name":"RETURNED","count":"20"},"5":{"id":"5","name":"FAILED","count":"46"},"58":{"id":"58","name":"REVISE","count":"197"}}}...
how to I remove the preceding 1, 3, 6 and 58 from array or JSON
I have tried array_values() but it is not converting the nested part of the array
how to i remove the preceding 1, 3, 6 and 58 from array or json
If you want json_encode() to return JSON array all the array keys must:
be numeric
be in sequence
the sequence must start from 0
For example:
$a = [1,2,3];
echo json_encode($a);
outputs desired
[1,2,3]
same with explicitly set indexes:
$a = [0=>1,2,3];
but
$a = [1=>1,2,3];
would output object:
{"1":1,"2":2,"3":3}
because sequence does not start from 0. Same for your case:
$a = [1,2,58=>3];
which produces
{"0":1,"1":2,"58":3}
because continuity of the key sequence is broken by index 58.
So depending on how you build your source array simply remove own keys with i.e. array_values():
$a = [1,2,58=>3];
echo json_encode(array_values($a)]);
would produce
[1,2,3]
so use array_values() on your $data['statuses'] and you are done.
Sorry if this question is worded incorrectly or doesn't make any sense. What I am trying to do is write an if statement that checks if:
array(6) {
[5]=>
string(17) "Quality Assurance"
[6]=>
string(7) "Analyst"
[7]=>
string(19) "Developer/Front end"
[8]=>
string(18) "Developer/Back end"
[9]=>
string(4) "Test"
[10]=>
string(2) "hi"
}
Any of those keys, in this case, 5, 6, 7, 8, 9, 10 is in:
array(4) {
[0]=>
object(stdClass)#195 (2) {
["labour_type_id"]=>
int(5)
["required_labour_type_hours"]=>
int(40)
}
[1]=>
object(stdClass)#193 (2) {
["labour_type_id"]=>
int(6)
["required_labour_type_hours"]=>
int(80)
}
}
This second arrays "labour_type_id".
In this example, 5 and 6 would match.
I am trying to use the in_array() function but I am not sure how to access the labour_type_id of the second array.
My best attempt at the moment:
#foreach($labourTypes as $id => $name)
#if(in_array($id, $reqLabourTypes->labour_type_id))
Where labourTypes is the first array, and reqLabourTypes is the 2nd array.
Thanks.
I've cleaned up this little search for you to try and find it as you require:
$new = array_filter(array_map(function(&$item) use($requiredLabour, $labourTypes){
$key = array_search($item, $labourTypes);
foreach($requiredLabour as $elem){
if($elem['labour_type_id'] == $key) {
return array(
$key => $item,
'options' => $elem
);
}
}
}, $labourTypes));
Everything will be accessible in $new if found. It returns:
Array
(
[5] => Array
(
[5] => Quality Assurance
[options] => Array
(
[labour_type_id] => 5
[required_labour_hours] => 40
)
)
[6] => Array
(
[6] => Analyst
[options] => Array
(
[labour_type_id] => 6
[required_labour_hours] => 40
)
)
)
The above is just the output, you can change it to whatever you need it to be by simply editing the return array(..... inside to whatever you require.
Example/Demo
i have two interesting arrays that im trying to combine together. Simple put:
$firstArr
array(3) {
[0] => array(2) {
[0] => string(1) "1"
[1] => string(16) "test1"
}
[1] => array(2) {
[0] => string(1) "8"
[1] => string(26) "test2"
}
[2] => array(2) {
[0] => string(1) "9"
[1] => string(23) "test3"
}
}
$secondArr
array(3) {
[0] => string(1) "1"
[1] => string(1) "2"
[2] => string(1) "3"
}
what i would like to get is something like this (not arrays):
$x = 1, 8, 8, 9, 9, 9;
$y = test1, test2, test2, test3, test3, test3;
basically the second array values dictates how many times the first array values are duplicated.
any ideas?
$firstArr=array(array("1","test1"),array("8","test2"),array("9","test3"));
$secondArr=array("1","2","3");
$x=$y='';
foreach ($secondArr as $k=>$v){
$x.= str_repeat($firstArr[$k][0].',',$v);
$y.= str_repeat($firstArr[$k][1].',',$v);
}
echo rtrim($x,",").";\n";
echo rtrim($y,",").";";
OUTPUT:
1,8,8,9,9,9;
test1,test2,test2,test3,test3,test3;
working demo:
http://codepad.org/yFvWs0Rh