Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
  • 0
Ninjonik

Spojenie viacerých Arrayov

Dotaz

Zdravím,
pokúšam sa spojiť 3 arraye do jedného no žiaľ z nejakého dôvodu sa po spojení týchto arrayov (3 arrayov) zostanú údaje iba z toho prvého arraya a tie ostatné nič, ako by som ich vôbec ani nespájal.
Zaujímalo by ma, že prečo to robí a ako to opraviť, keďže túto istú funkciu PHP využívam aj inde a tam spájanie arrayov normálne funguje.
Skúšal som aj funkcie array_merge(), array_merge_recursive(), ale nič z toho nefunguje ako má.

Ja vlastne mám 3 arraye, ktoré potrebujem spojiť do jedného, pretože každý z tých arrayov obsahuje niečo, čo potrebujem mať vo finálnom arrayi.

Pošlem rovno celý kód, keby sa náhodou hodil.
 

	$stmt25 = $db->prepare("SELECT * FROM posts WHERE sectionID=:sectionID ORDER BY id DESC");
	$stmt25->execute(array(":sectionID" => $row["sectionID"]));
	$row25 = $stmt25->fetch(PDO::FETCH_ASSOC);

	if (!$stmt25->execute())
	{
	  print_r($stmt25->errorInfo());
	}

	while ($row25 = $stmt25->fetch(PDO::FETCH_ASSOC))
	{
		$rowfirst[] = $row25;

		//

		$stmt255 = $db->prepare("SELECT * FROM categories WHERE catID=:catID");
		$stmt255->execute(array(":catID" => $row25["sectionID"]));
		$row255 = $stmt255->fetch(PDO::FETCH_ASSOC);

		if (!$stmt255->execute())
		{
			print_r($stmt255->errorInfo());
		}

		while ($row255 = $stmt255->fetch(PDO::FETCH_ASSOC))
		{
			$rowsecond[] = $row255;

			//

			$stmt2556 = $db->prepare("SELECT username, avatar FROM members WHERE memberID=:memberID");
			$stmt2556->execute(array(":memberID" => $row25["authorID"]));
			$row2556 = $stmt2556->fetch(PDO::FETCH_ASSOC);

			if (!$stmt2556->execute())
			{
				print_r($stmt2556->errorInfo());
			}

			while ($row2556 = $stmt2556->fetch(PDO::FETCH_ASSOC))
			{
				$rowthird[] = $row2556;
			}

			//

		}

		//

	}

	$rowfinal23 = $rowfirst + $rowsecond + $rowthird;

No a po dumpnutí tohto arrayu to vyzerá takto:
 

 array(5) {
  [0]=>
  array(10) {
    ["id"]=>
    int(6)
    ["title"]=>
    string(6) "Post 6"
    ["authorID"]=>
    int(9)
    ["sectionID"]=>
    int(1)
    ["text"]=>
    string(25) "<p>Post 6 Description</p>"
    ["date"]=>
    int(1625851255)
    ["editdate"]=>
    int(1625851255)
    ["isallowed"]=>
    int(0)
    ["isuser"]=>
    int(1)
    ["background"]=>
    string(14) "1625851255.jpg"
  }
  [1]=>
  array(10) {
    ["id"]=>
    int(5)
    ["title"]=>
    string(6) "Post 5"
    ["authorID"]=>
    int(9)
    ["sectionID"]=>
    int(1)
    ["text"]=>
    string(25) "<p>Post 5 Description</p>"
    ["date"]=>
    int(1625849226)
    ["editdate"]=>
    int(1625849226)
    ["isallowed"]=>
    int(0)
    ["isuser"]=>
    int(1)
    ["background"]=>
    string(14) "1625851255.jpg"
  }
  [2]=>
  array(10) {
    ["id"]=>
    int(4)
    ["title"]=>
    string(6) "Post 4"
    ["authorID"]=>
    int(9)
    ["sectionID"]=>
    int(1)
    ["text"]=>
    string(25) "<p>Post 4 Description</p>"
    ["date"]=>
    int(1625769722)
    ["editdate"]=>
    int(1625769722)
    ["isallowed"]=>
    int(0)
    ["isuser"]=>
    int(1)
    ["background"]=>
    string(14) "1625769723.jpg"
  }
  [3]=>
  array(10) {
    ["id"]=>
    int(3)
    ["title"]=>
    string(6) "Post 3"
    ["authorID"]=>
    int(9)
    ["sectionID"]=>
    int(1)
    ["text"]=>
    string(25) "<p>Post 3 Description</p>"
    ["date"]=>
    int(1625684695)
    ["editdate"]=>
    int(1625684695)
    ["isallowed"]=>
    int(0)
    ["isuser"]=>
    int(1)
    ["background"]=>
    string(14) "1625851255.jpg"
  }
  [4]=>
  array(10) {
    ["id"]=>
    int(1)
    ["title"]=>
    string(6) "Post 1"
    ["authorID"]=>
    int(9)
    ["sectionID"]=>
    int(1)
    ["text"]=>
    string(25) "<p>Post 1 Description</p>"
    ["date"]=>
    int(1625498415)
    ["editdate"]=>
    int(1625498415)
    ["isallowed"]=>
    int(0)
    ["isuser"]=>
    int(0)
    ["background"]=>
    string(14) "1625851255.jpg"
  }
}

Takto vyzerá $rowfirst array:
 

                                  array(5) {
  [0]=>
  array(10) {
    ["id"]=>
    int(6)
    ["title"]=>
    string(6) "Post 6"
    ["authorID"]=>
    int(9)
    ["sectionID"]=>
    int(1)
    ["text"]=>
    string(25) "<p>Post 6 Description</p>"
    ["date"]=>
    int(1625851255)
    ["editdate"]=>
    int(1625851255)
    ["isallowed"]=>
    int(0)
    ["isuser"]=>
    int(1)
    ["background"]=>
    string(14) "1625851255.jpg"
  }
  [1]=>
  array(10) {
    ["id"]=>
    int(5)
    ["title"]=>
    string(6) "Post 5"
    ["authorID"]=>
    int(9)
    ["sectionID"]=>
    int(1)
    ["text"]=>
    string(25) "<p>Post 5 Description</p>"
    ["date"]=>
    int(1625849226)
    ["editdate"]=>
    int(1625849226)
    ["isallowed"]=>
    int(0)
    ["isuser"]=>
    int(1)
    ["background"]=>
    string(14) "1625851255.jpg"
  }
  [2]=>
  array(10) {
    ["id"]=>
    int(4)
    ["title"]=>
    string(6) "Post 4"
    ["authorID"]=>
    int(9)
    ["sectionID"]=>
    int(1)
    ["text"]=>
    string(25) "<p>Post 4 Description</p>"
    ["date"]=>
    int(1625769722)
    ["editdate"]=>
    int(1625769722)
    ["isallowed"]=>
    int(0)
    ["isuser"]=>
    int(1)
    ["background"]=>
    string(14) "1625769723.jpg"
  }
  [3]=>
  array(10) {
    ["id"]=>
    int(3)
    ["title"]=>
    string(6) "Post 3"
    ["authorID"]=>
    int(9)
    ["sectionID"]=>
    int(1)
    ["text"]=>
    string(25) "<p>Post 3 Description</p>"
    ["date"]=>
    int(1625684695)
    ["editdate"]=>
    int(1625684695)
    ["isallowed"]=>
    int(0)
    ["isuser"]=>
    int(1)
    ["background"]=>
    string(14) "1625851255.jpg"
  }
  [4]=>
  array(10) {
    ["id"]=>
    int(1)
    ["title"]=>
    string(6) "Post 1"
    ["authorID"]=>
    int(9)
    ["sectionID"]=>
    int(1)
    ["text"]=>
    string(25) "<p>Post 1 Description</p>"
    ["date"]=>
    int(1625498415)
    ["editdate"]=>
    int(1625498415)
    ["isallowed"]=>
    int(0)
    ["isuser"]=>
    int(0)
    ["background"]=>
    string(14) "1625851255.jpg"
  }
}

$rowsecond array:
 

array(5) {
  [0]=>
  array(2) {
    ["catID"]=>
    int(1)
    ["name"]=>
    string(10) "Category 1"
  }
  [1]=>
  array(2) {
    ["catID"]=>
    int(1)
    ["name"]=>
    string(10) "Category 1"
  }
  [2]=>
  array(2) {
    ["catID"]=>
    int(1)
    ["name"]=>
    string(10) "Category 1"
  }
  [3]=>
  array(2) {
    ["catID"]=>
    int(1)
    ["name"]=>
    string(10) "Category 1"
  }
  [4]=>
  array(2) {
    ["catID"]=>
    int(1)
    ["name"]=>
    string(10) "Category 1"
  }
}

no a $rowthird:
 

array(5) {
  [0]=>
  array(2) {
    ["username"]=>
    string(11) "NinjonikSVK"
    ["avatar"]=>
    string(14) "1612860711.png"
  }
  [1]=>
  array(2) {
    ["username"]=>
    string(11) "NinjonikSVK"
    ["avatar"]=>
    string(14) "1612860711.png"
  }
  [2]=>
  array(2) {
    ["username"]=>
    string(11) "NinjonikSVK"
    ["avatar"]=>
    string(14) "1612860711.png"
  }
  [3]=>
  array(2) {
    ["username"]=>
    string(11) "NinjonikSVK"
    ["avatar"]=>
    string(14) "1612860711.png"
  }
  [4]=>
  array(2) {
    ["username"]=>
    string(11) "NinjonikSVK"
    ["avatar"]=>
    string(14) "1612860711.png"
  }
}

Ako som písal, nemám ani poňatia, že prečo to nefunguje a nepodarilo sa mi nájsť odpoveď ani na iných fórach, tak preto to skúsim tu.
Predom za pomoc ďakujem.

Sdílet tento příspěvek


Link to post
Share on other sites

2 odpovědí na tuto otázku

Recommended Posts

  • 1

Spojení array pomocí $arr + $arr nepočítá se rekurzivitou, jinak by to šlo.

Spojení pomocí array_merge_recursive funguje pouze v případě, pokud máš non-numeric keys.

https://www.php.net/manual/en/function.array-merge-recursive.php

Citace

If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.

Tudíž nejjednodušší řešení by bylo buď indexovat array stringem třeba jako: 'primarní klíč' => [...]

nebo na netu sehnat nějaký snippet (nebo si vytvořit), který by tento merge s numerickejma klíčema zvládl.

Příklad řešení:

$arr1 = [
	['id' => 1],
	['id' => 2],
];
$arr2 = [
	['cat_id' => 3],
	['cat_id' => 4],
];
$arr3 = [
	['usr_id' => 5],
	['usr_id' => 6],
];

$arrFinal = array_merge_recursive($arr1, $arr2, $arr3);
var_dump($arrFinal);

OUTPUT - pouze append:
array(6) { [0]=> array(1) { ["id"]=> int(1) } [1]=> array(1) { ["id"]=> int(2) } [2]=> array(1) { ["cat_id"]=> int(3) } [3]=> array(1) { ["cat_id"]=> int(4) } [4]=> array(1) { ["usr_id"]=> int(5) } [5]=> array(1) { ["usr_id"]=> int(6) } } 
$arr1 = [
	'i1' => ['id' => 1],
	'i1' => ['id' => 2],
];
$arr2 = [
	'i1' => ['cat_id' => 3],
	'i2' => ['cat_id' => 4],
];
$arr3 = [
	'i1' => ['usr_id' => 5],
	'i2' => ['usr_id' => 6],
];

$arrFinal = array_merge_recursive($arr1, $arr2, $arr3);
var_dump($arrFinal);

OUTPUT - merge:
array(2) { ["i1"]=> array(3) { ["id"]=> int(2) ["cat_id"]=> int(3) ["usr_id"]=> int(5) } ["i2"]=> array(2) { ["cat_id"]=> int(4) ["usr_id"]=> int(6) } } 

 

Sdílet tento příspěvek


Link to post
Share on other sites
  • 0
Author of the topic Odesláno: (upraveno)
On 15. 7. 2021 at 16:03, mxco said:

Spojení array pomocí $arr + $arr nepočítá se rekurzivitou, jinak by to šlo.

Spojení pomocí array_merge_recursive funguje pouze v případě, pokud máš non-numeric keys.

https://www.php.net/manual/en/function.array-merge-recursive.php

Tudíž nejjednodušší řešení by bylo buď indexovat array stringem třeba jako: 'primarní klíč' => [...]

nebo na netu sehnat nějaký snippet (nebo si vytvořit), který by tento merge s numerickejma klíčema zvládl.

Příklad řešení:

$arr1 = [
	['id' => 1],
	['id' => 2],
];
$arr2 = [
	['cat_id' => 3],
	['cat_id' => 4],
];
$arr3 = [
	['usr_id' => 5],
	['usr_id' => 6],
];

$arrFinal = array_merge_recursive($arr1, $arr2, $arr3);
var_dump($arrFinal);

OUTPUT - pouze append:
array(6) { [0]=> array(1) { ["id"]=> int(1) } [1]=> array(1) { ["id"]=> int(2) } [2]=> array(1) { ["cat_id"]=> int(3) } [3]=> array(1) { ["cat_id"]=> int(4) } [4]=> array(1) { ["usr_id"]=> int(5) } [5]=> array(1) { ["usr_id"]=> int(6) } } 
$arr1 = [
	'i1' => ['id' => 1],
	'i1' => ['id' => 2],
];
$arr2 = [
	'i1' => ['cat_id' => 3],
	'i2' => ['cat_id' => 4],
];
$arr3 = [
	'i1' => ['usr_id' => 5],
	'i2' => ['usr_id' => 6],
];

$arrFinal = array_merge_recursive($arr1, $arr2, $arr3);
var_dump($arrFinal);

OUTPUT - merge:
array(2) { ["i1"]=> array(3) { ["id"]=> int(2) ["cat_id"]=> int(3) ["usr_id"]=> int(5) } ["i2"]=> array(2) { ["cat_id"]=> int(4) ["usr_id"]=> int(6) } } 

 

Ďakujem za pomoc, snáď to niekedy využijem, ale medzi tým som ešte hľadal aj iné spôsoby ako by sa to dalo spraviť a prišiel som na to, že mi stačí všetky moje SQL podnety len dať do jedného:
 

SELECT
    P.id,
    P.title,
    P.text,
    P.editdate,
    P.background,
    C.name,
    M.username,
    M.avatar
FROM
    posts AS P
INNER JOIN categories AS C
ON
    C.catID = P.sectionID
INNER JOIN members AS M
ON
    M.memberID = P.authorID
WHERE P.id=1

Ak by sa to ešte niekomu hodilo, tak to tu priložím.

Edited by Ninjonik

Sdílet tento příspěvek


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...