Dissect, sort and compare arrays [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have the two following arrays:
a:4:{i:0;s:2:"98";i:1;s:2:"98";i:2;s:2:"89";i:3;s:2:"99";}
a:4:{i:0;s:11:"Musculation";i:1;s:3:"Gym";i:2;s:22:"Production in HTML/CSS";i:3;s:9:"Endurance";}
Each array has 4 values that are correlated. I want to display only three values from second array that have the highest correlating number values from first array:-
Endurance - 99
Musculation - 98
Gym - 98
How do I achieve this?

Use array_multisort to sort the text according to the corresponding numbers
array_multisort($numbers, SORT_DESC, SORT_NUMERIC, $text);
Take the first three values.
$result = array_slice($text, 0, 3);
If you want to show the numbers with the text, the keys will still match up, so you can iterate the text array and use its key to get the right value from the number array.
foreach ($text as $key => $title) {
echo "$title: $numbers[$key]\n";
}

Related

How to sum two rows input file numbers to one row output file (PHP) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 months ago.
Improve this question
Input data:
In the input file INPUT.TXT there are two non-negative integers are given by two rows and the numbers are less than 10 powered by 100.
Output data:
in the OUTPUT.TXT file is needed to return sum of the numbers to one row, without initial zeros
Example:
#
input.txt
output.txt
1
3
7
4
Check it , It will help you
$data = explode("\n", file_get_contents('INPUT.TXT'));
$sum = intval($data[0])+intval($data[1]);
echo $sum;
file_put_contents('OUTPUT.TXT', $sum);

I have two arrays. In first array i have some null values and i want to update these null values with second array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
In this I have two arrays, In the first array I have some key have null values in the array. in the second array, I have also the same number of keys in arrays as having null values in first, I want to replace the first array key null values with the second array values
$arr1=array(0=>array('quantity'=>1),1=>array(),2=>array('quantity'=>3),3=>array(),4=>array());
$arr2=array (0 =>array(),1 =>array (),2 =>array(0 =>array('quantity'=>2)));
$result_array_needed=array(0=>array('quantity'=>1),1=>array(),2=>array('quantity'=>3),3=>array(),4=>array(0 =>array('quantity'=>2)));
Nick answer is good. I think you can simplify it using array-shift:
foreach ($arr1 as &$arr)
if (empty($arr)) $arr = array_shift($arr2);
Live example: 3v4l

How to get certain part of a string value in php? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I get one value like "13and45" and would like to know if there is a way so I can store these values separately like
$string="13and45";
$value1=13;
$value2=45;
Do you guys know how I can manipulate the string in order to get something like what I have above?
PHP - Explode () function
This will return an array with the two values.
Example:
$array = explode ("and", $string);
Returned array with strings:
$array[0] = 13
$array[1] = 45
You can use something like intval(substr($string, 0, 2) to get the first integer and intval(substr($string, 5, 2) for the second. intval will get the integer value from a string, and substr will get the portion of the string given by ($string, index, length).

How Count Array [closed]

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 6 years ago.
Improve this question
If array with 2 keys:
Array(
[0] => content
[1] => content
)
It is a task
if array 3 keys
Array(
[0] => content
[1] => content
[2] => content
)
It is other task
How count how many keys ([0], [1]..) there are in the array?
I already have all the structure of the code but just need to know how to know the amount of keys within the variable that contains the array
$len = count($myArray);
I think this is what you're asking?
As per your comment if you are using array_count_values() it will return the array that contains no of count of same values.
$yourArr = array("content","content");
print_r( array_count_values($yourArr) );
Result:
array("content"=>2);
From W3School:
Returns an associative array, where the keys are the original array's values, and the values are the number of occurrences

Convert array to associative array with specific key and value [closed]

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 need to convert an array into associative array as first element should act as a key and second element should act as its value ?please tell me how i can do that
If you have only two elements (first and second as you mentioned), then you can do simply like this
$assoc = array($simple[0] => $simple[1]);
If you wanted to convert pairs of values like [1,2,3,4] to [1=>2,3=>4], then use this code snippet
$assoc = array();
for ($i = 0; $i < count($simple); $i=$i+2) {
$assoc[$simple[$i]] = $simple[$i+1];
}

Categories