sigmahokies Posted October 16, 2019 Share Posted October 16, 2019 (edited) <?php $srs = array(); for ($section = 1; $section < 5; $section++) { for ($row = $section; $row < 10; $row++) { for ($seat = $row; $seat < 20; $seat++) { $srs[] = array( 'section_name' => $section, 'row_name' => $row, 'seat_name' => $seat); } } } output(convert_array($srs)); // Converts the array function convert_array($input) { return $input; } function output($obj) { echo "<pre>"; print_r($obj); echo "</pre>"; die; } ?> I'm trying to convert those array number to string, add implode() to make it happen, but seem PHP didn't recognize variable in $input. Like this implode("Section: ", $input) or (Section: ", $srs). Can you help me? Thanks, Gary Edited October 16, 2019 by sigmahokies Quote Link to comment Share on other sites More sharing options...
Barand Posted October 16, 2019 Share Posted October 16, 2019 echo json_encode($srs); If that's not what you want then please provide an example of what you want to end up with. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted October 16, 2019 Share Posted October 16, 2019 32 minutes ago, sigmahokies said: I'm trying to convert those array number to string, add implode() to make it happen, but seem PHP didn't recognize variable in $input. $srs is an array of arrays. To use implode(), you'll need to loop through the outer array. Then you can use implode() to output the section information. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 16, 2019 Share Posted October 16, 2019 Yeah, you need to provide clarification. I';ve read your code and your request and am not sure what you are trying to accomplish. I see that you are creating some "random" records to specify "seat" which include three properties: section, row and seat no. Are you wanting to condense these so you can show a series of seats in the same section/row? E.g. "Section 4, Row 8, Seats 6-10"? Or are you just wanting to output each record in a human readable format: "Section 4, Row 8, Seats 6", "Section 4, Row 8, Seats 7", "Section 4, Row 8, Seats 8", etc.? If it is the former, I would structure the array differently. If the latter, then just loop over the array foreach($arrayVar as $seat) { echo "Section: {$seat['section_name']}, Row: {$seat['row_name']}, Seat: {$seat['seat_name']}<br>"; } Quote Link to comment Share on other sites More sharing options...
sigmahokies Posted October 17, 2019 Author Share Posted October 17, 2019 (edited) All right, I'm trying to tell you that I'm trying to get all array from $srs in above of script into function below of loop in array, but problem is PHP still does not recognize $srs is defined from beginning of script; Look at image screen as example that need to be done. Edited October 17, 2019 by sigmahokies Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted October 17, 2019 Share Posted October 17, 2019 6 minutes ago, sigmahokies said: ...not recognize $srs is defined from beginning of script... Based on the initial code above, $srs is passed to the output() function. In that function, the passed value from $srs is now stored in $obj, since that's what you named the function argument. Due to variable scope, $srs isn't going to be available in the function. However, you can use $obj to refer to the information. That's the variable you would use to perform the loop and output the data as you see fit. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 17, 2019 Share Posted October 17, 2019 (edited) 30 minutes ago, sigmahokies said: All right, I'm trying to tell you that I'm trying to get all array from $srs in above of script into function below of loop in array, but problem is PHP still does not recognize $srs is defined from beginning of script; Look at image screen as example that need to be done. Please read that statement back to yourself. I really don't care if someone uses poor grammar or has misspellings - as long as what they are stating is understood. I don't understand that. I *think* you might be stating that the variable $srs is not being recognized in the convert_array() function. If so, why did you not post the error? It will tell you the line number that the error was thrown on. I ran the code you posted and it executed w/o error - but it is not a multi-dimensional array as you last posted. I have the following in the signature of my posts "The quality of the responses received is directly proportional to the quality of the question asked." I find it rather annoying when people come to ask for free help and can't take the time to clearly define what it is they want. I still don't know what you are wanting to achieve. Are you wanting to convert the single-dimensional array to a multi-dimensional array? If so, why don't you create the array as multi-dimensional to begin with? Also, why does each successive for() loop use the ending value from the previous loop as the stating value? I see no logical reason why "Section 1" would have rows 1-5, "Section 2: would have rows 2-6, "Section 3" would have rows 3-7, etc. None of this makes any sense. 17 minutes ago, cyberRobot said: Based on the initial code above, $srs is passed to the output() function. In that function, the passed value from $srs is now stored in $obj, since that's what you named the function argument. Due to variable scope, $srs isn't going to be available in the function. However, you can use $obj to refer to the information. That's the variable you would use to perform the loop and output the data as you see fit. No. $srs is passed to the convert_array() function which simply returns back the original value passed into it. The use of that function with $srs is used as the parameter for the output() function. The end result is that an array identical to $srs is passed into the output() function - which simply dumps the contents of the array to the screen. I don't see anywhere he is trying reference $srs in the convert_array() function. The code "works" without errors. Edited October 17, 2019 by Psycho Quote Link to comment Share on other sites More sharing options...
Barand Posted October 17, 2019 Share Posted October 17, 2019 Perhaps you want something like this $srs = []; foreach (range('A', 'D') as $section) { $srs['sections'][$section] = ['rows' => []]; for ($row = 1; $row < 3; $row++) { $srs['sections'][$section]['rows'][$row] = ['seats' => []]; for ($seat = 1; $seat < 4; $seat++) { $srs['sections'][$section]['rows'][$row]['seats'][] = $seat; } } } echo '<pre>', print_r($srs, 1), '</pre>'; Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted October 17, 2019 Share Posted October 17, 2019 5 minutes ago, Psycho said: No. $srs is passed to the convert_array() function Yeah, I just noticed that after reading your post. I didn't notice the output discrepancy until after making my post. I was in the middle of writing something when your post came through. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 17, 2019 Share Posted October 17, 2019 (edited) @Barand I was thinking the same thing - just build the array the way you need it. But, it's also possible that the loops at the top are just to create some mock data as it would be returned from a DB query and the reason he needs a convert() function is to then turn those DB results into a logically structured array. Either solution could have very easily been provided in the very first response if the problem was explained clearly. But, it's impossible to know without consulting the crystal ball 🔮 EDIT: Also, I'm not sure when it changed, but you can define a "child" array element directly w/o having to define the parent elements first. This used to produce a warning, but has not been the case for a long time now. So, while I used to do just as you did above (and sometimes still do), that can be simplified to just this foreach (range('A', 'D') as $section) { for ($row = 1; $row < 3; $row++) { for ($seat = 1; $seat < 4; $seat++) { $srs['sections'][$section]['rows'][$row]['seats'][] = $seat; } } } Edited October 17, 2019 by Psycho Quote Link to comment Share on other sites More sharing options...
Barand Posted October 17, 2019 Share Posted October 17, 2019 I've just written a convert_array() function that uses that easier method $output = convert_array($srs); function convert_array($arr) { $new = []; foreach ($arr as $rec) { $new['sections'][$rec['section_name']]['rows'][$rec['row_name']]['seats'][] = $rec['seat_name']; } return $new; } Quote Link to comment Share on other sites More sharing options...
Barand Posted October 17, 2019 Share Posted October 17, 2019 However If you are going to restructure your array then IMHO it would make more sense if you just had ... Array ( [A] => Array ( [1] => Array ( [1] => 0 [2] => 0 [3] => 0 [4] => 0 ) [2] => Array ( [2] => 0 [3] => 0 [4] => 0 ) ) … where you can access a seat value with $val = $srs[$section][$row][$seat]; 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.