Help please style 2 arrays of arrays: There are 2 arrays:

array:19 [▼ 0 => array:3 [▼ 0 => "Арендатор" 1 => "Возвраты<br>(РУБ, ВКЛ. НДС)" 2 => "Возвраты<br>(ШТ.)" ] 1 => array:3 [▼ 0 => "one" 1 => "-25 900,00" 2 => -7.0 ] 2 => array:3 [▼ 0 => "two" 1 => "-22 498,00" 2 => -2.0 ] ] 

and the second array

 array:25 [▼ 0 => array:10 [▼ 0 => "Арендатор" 1 => "Продажи<br><span>(руб, вкл. ндс)</span>" 2 => "Продажи<br><span>(шт.)</span>" 3 => "Возвраты<br><span>(руб, вкл. ндс)</span>" 4 => "Возвраты<br><span>(шт.)</span>" 5 => "Продажи<br>с учетом возвратов<br><span>(руб, вкл. ндс)</span>" 6 => "Продажи<br>с учетом возвратов<br><span>(шт.)" 7 => "Ср. ст-ть товара<br><span>(руб, вкл. ндс)" 8 => "Кол-во чеков<br><span>(шт.)</span>" 9 => "Средний чек<br><span>(руб, вкл. ндс)</span>" ] 1 => array:10 [▼ 0 => "<a href="/dashboard?date_start=2018-02-01&date_end=2018-02-23&as_user=two">two</a>" 1 => "" 2 => "" 3 => "" 4 => "" 5 => "526 077,00" 6 => 270.0 7 => "1 948,43" 8 => 171 9 => "3 076,47" ] 2 => array:10 [▼ 0 => "<a href="/dashboard?date_start=2018-02-01&date_end=2018-02-23&as_user=one">one</a>" 1 => "" 2 => "" 3 => "" 4 => "" 5 => "13 340,00" 6 => 10.0 7 => "1 334,00" 8 => 10 9 => "1 334,00" ] 

I need in the second array in the 3rd and 4th row to write data from 1 array (1st and 2nd row, respectively), provided that the name in the second (0-string, that part that is enclosed in <a></a> ) corresponds the name in the first array (also zero line)

I understand that you need to create a new array and loop through both arrays and write to a new one, but in fact some errors take off.

  $merge = []; for ($i = 0; $i < count($array['sells']); $i++) { $merge[$i] = $array['sells'][$i]; if(in_array(substr(strstr($merge[$i][0], '>'), 1, -4),$array['refunds'][$i][0] )){ $merge[$i][4] = $array['refunds'][$i][2]; } } 

I did this, but $ array ['refunds'] [$ i] [0] produces strings, not an array. I can’t understand, so paste the code.

  • Good morning. So show your code and show errors. - user216615
  • @peter Stack Overflow in Russian Meta $ merge = []; for ($ i = 0; $ i <count ($ array ['sells']); $ i ++) {$ merge [$ i] = $ array ['sells'] [$ i]; if (in_array (substr (strstr ($ merge [$ i] [0], '>'), 1, -4), $ array ['refunds'] [$ i] [0])) {$ merge [$ i] [4] = $ array ['refunds'] [$ i] [2]; }} Stack Overflow in Russian Meta I did this, but $ array ['refunds'] [$ i] [0] produces lines, not an array. I can’t understand, so paste the code: / - Vladislav

2 answers 2

take the first array $first , extract the values ​​of the 0th column, the values ​​of which you need to check for a match. Then create a new array, where these 0-values ​​will become keys.

  $keys = array_column($first, 0); $first = array_combine($keys, $first); 

then go through the second array and check the zero value. If there you need <a> , then extract the value, and check if it is in the first one. If so, replace it.

 foreach($second as &$v){ if(preg_match("/.*>(.*?)<\/a>/", $v[0], $matches)){ $k = $matches[1]; if(isset($first[$k]){ $v[3] = $first[$k][1]; $v[4] = $first[$k][2]; } } } 

probably something like this. ( code is approximate, not verified )

  • Isn't it easier to do strip_tags ? For the regular season place. - And
  • @And if I was sure that only the <a> tag could be located there and nothing else, it would have been so. However, in the first element, just the text - it is not necessary to check it, as I understand it. - teran
  • @Vladislav, avoid the code in the comments, it is not possible to read. Complete your question. So that it was immediately obvious that they tried and tried, but generally, a normal array was working, but I don’t need a dump, I just don’t want to rewrite it manually. - And
  • It is possible and stripos($str, '<a') to search yuznut. By the same clearly visible link. - And
  • $ merge = []; for ($ i = 0; $ i <count ($ array ['sells']); $ i ++) {$ merge [$ i] = $ array ['sells'] [$ i]; if (in_array (substr (strstr ($ merge [$ i] [0], '>'), 1, -4), $ array ['refunds'] [$ i] [0])) {$ merge [$ i] [4] = $ array ['refunds'] [$ i] [2]; }} I’m already exhausted, I’m not inserting it as a code. Problem in $ array ['refunds'] [$ i] [0] - Vladislav
 $array_user = []; foreach ($array['refunds'] as $user) { $array_user[$user[0]] = array($user[1],$user[2]); }; foreach ($array['sells'] as $key=> $user) { if(isset($array_user[substr(strstr($user[0], '>'), 1, -4)])) { $array['sells'][$key][1] = number_format((float)str_replace(" ", "", $array_user[substr(strstr($user[0], '>'), 1, -4)][0])*(-1) + (float)str_replace(" ", "", $array['sells'][$key][5]), 2, ',', ' '); $array['sells'][$key][2] = (intval($array_user[substr(strstr($user[0], '>'), 1, -4)][1]) - intval($array['sells'][$key][6]))*(-1); $array['sells'][$key][3] = $array_user[substr(strstr($user[0], '>'), 1, -4)][0]; $array['sells'][$key][4] = $array_user[substr(strstr($user[0], '>'), 1, -4)][1]; } }