Jump to content

Convert array to string


sigmahokies

Recommended Posts

	<?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 by sigmahokies
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>";
}

 

Link to comment
Share on other sites

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.

 

 

Screen Shot 2019-10-17 at 8.25.25 AM.png

Edited by sigmahokies
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Psycho
Link to comment
Share on other sites

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>';

 

Link to comment
Share on other sites

@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 by Psycho
Link to comment
Share on other sites

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];

 

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.